题目
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10, M = N*2;
int h[N], e[M], ne[M], idx;
int n;
int ans = 2e9;
bool st[N];
void add(int a, int b) // 添加一条边a->b
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
int dfs(int u)
{
int maxx = 0; int sz = 0; st[u] = true;
for(int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if(st[j]) continue;
int s = dfs(j);
maxx = max(maxx, s);
sz += s;
}
maxx = max(maxx, n-sz-1);
ans = min(ans, maxx);
return sz+1;
}
int main()
{
cin >> n;
memset(h, -1, sizeof h);
for(int i = 0; i < n-1; i++)
{
int a, b;
cin >> a >> b;
add(a, b);
add(b, a);
}
dfs(1);
cout << ans;
}
注意