树的重心
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 100010;
int h[N], ne[2 * N], e[2 * N], idx;
bool st[N];
int n;
int res = N;
void add(int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx ++ ;
}
int dfs(int u) {
int size = 0;
int sum = 1;
st[u] = true;
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (st[j]) continue;
int s = dfs(j);
size = max(size, s);//size是以u为根的最大子树的节点数
sum += s ; //sum是以u为根节点的所有子树的节点数
}
res = min(res, max(size, n - sum));
return sum;
}
int main() {
memset(h, -1, sizeof h);
int a, b;
cin >> n;
for (int i = 0; i < n - 1; i ++ ) {
cin >> a >> b;
add(a, b);
add(b, a);
}
dfs(1);
cout << res << endl;
return 0;
}