#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+10,M=N*2;
int h[N],e[M],ne[M],idx;
bool st[N];
int n,m,res=1e9;
int f[N];
int pos;
void init(){
memset(h,-1,sizeof h);
idx=0;
}
void add(int a,int b){
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
void dfs(int u){
st[u]=1;
f[u]=1;
int max_son=0;
for(int i=h[u];i!=-1;i=ne[i]){
int j=e[i];
if(!st[j]){
dfs(j);
f[u]+=f[j];
max_son=max(max_son,f[j]);
}
}
max_son=max(max_son,n-f[u]);
if(res>max_son){
res=max_son;
pos=u;
}
}
int main(){
cin>>n;
init();
for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
add(a,b);
add(b,a);
}
dfs(1);
cout<<res<<endl;
return 0;
}
树的重心【dfs】【数组模拟链表】
最新推荐文章于 2023-02-26 17:33:12 发布