#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题看这个代码错哪了?
全部评论
(0) 回帖