F题

#include <iostream>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <deque>
#include <cstdlib>
#define lowbit(x) ((x) & -(x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
const int maxn = 1e6 + 7;
const int INF = 0x3f3f3f3f;
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}
vector<int>v[maxn];
int ans;
int fa[maxn];
int find(int x){
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void union_set(int x,int y){
    fa[find(x)] = find(y);
}
void dfs(int root){
    if (v[root].empty())
        ans ++;
    int len = v[root].size();
    for (int i = 0; i < len; i ++){
        dfs(v[root][i]);
    }
}
int main(){
    //ios::sync_with_stdio(false);
    //freopen("text.txt","r",stdin);
    //freopen("out1.txt","w",stdout);
    int n;
    while(cin >> n){
        ans = 0;
        for (int i = 1; i <= n; i ++)
            fa[i] = i, v[i].clear();
        for (int i = 1; i < n; i ++){
            int x, y;
            x = read();
            y = read();
            union_set(y,x);
            v[x].push_back(y);
        }
        int root = find(1);
        dfs(root);
        if (v[root].size() == 1)
            ans ++;
        cout << ans << endl;
    }
    return 0;
}
蹲个大佬,F题看这个代码错哪了?
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:02
好不容易拿到了字节Offer,鼠鼠做后端的,但家里人觉得可能被裁员不稳定,让鼠鼠去投国企,现在好纠结到底该咋选
文档传偷助手:该投就投吧,不过建议别放弃offer 拿到手里的才是最好的
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
07-03 16:02
门头沟学院 Java
今天面试,非常紧张,面试官问我springboot有哪些核心模块都答不上来了,真的对自己无语了!
程序员小白条:28届我勒个去,很多人面试都没机会
查看1道真题和解析
点赞 评论 收藏
分享
真烦好烦真烦:豆包润色了自己没看看吗,再说了,都说豆包是愚蠢且勤快的大学生,ds才是聪明的研究生,怎么敢让豆包写论文的
你们的毕业论文什么进度了
点赞 评论 收藏
分享
仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务