http://acdream.info/contest?cid=1477#problem-A
找树的重心
Problem Description
Our country is tree-like structure, that is to say that N cities is connected by exactly N - 1 roads.
The old king has two little sons. To make everything fairly, he dicided to divide the country into two parts and each son get one part. Two sons can choose one city as their capitals. For each city, who will be their king is all depend on whose capital is more close to them. If two capitals have the same distance to them, they will choose the elder son as their king.
(The distance is the number of roads between two city)
The old king like his elder son more, so the elder son could choose his capital firstly. Everybody is selfish, the elder son want to own more cities after the little son choose capital while the little son also want to own the cities as much as he can.
If two sons both use optimal strategy, we wonder how many cities will choose elder son as their king.
The old king has two little sons. To make everything fairly, he dicided to divide the country into two parts and each son get one part. Two sons can choose one city as their capitals. For each city, who will be their king is all depend on whose capital is more close to them. If two capitals have the same distance to them, they will choose the elder son as their king.
(The distance is the number of roads between two city)
The old king like his elder son more, so the elder son could choose his capital firstly. Everybody is selfish, the elder son want to own more cities after the little son choose capital while the little son also want to own the cities as much as he can.
If two sons both use optimal strategy, we wonder how many cities will choose elder son as their king.
Input
There are multiple test cases.
The first line contains an integer N (1 ≤ N ≤ 50000).
The next N - 1 lines each line contains two integers a and b indicating there is a road between city a and city b. (1 ≤ a, b ≤ N)
The first line contains an integer N (1 ≤ N ≤ 50000).
The next N - 1 lines each line contains two integers a and b indicating there is a road between city a and city b. (1 ≤ a, b ≤ N)
Output
For each test case, output an integer indicating the number of cities who will choose elder son as their king.
Sample Input
4 1 2 2 3 3 4 4 1 2 1 3 1 4
Sample Output
2 3
/*
思路就是a选了一个点,把这个点当成一个根节点来看,那么b只能在a的子树中来选,为了能获得更多,
一定会选择a的子树中最多节点数的子树的根节点。那么为了使a得到最大,求每个点作为根节点时,
其节点最多子树的最小值即可。
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=5e4+10;
int cnt;
int head[maxn*2];
struct Edge{
int v;
int next;
}edge[maxn*2];
void add_edge(int u,int v){
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
// printf("u:%d v:%d\n",u,v);
}
int vis[maxn];
int sum[maxn];
int max_down[maxn];
void dfs(int now){
// printf("now:%d\n",now);
sum[now]=1;vis[now]=1;
for(int i=head[now];i!=-1;i=edge[i].next){
if(vis[edge[i].v])
continue;
// printf("v:%d\n",edge[i].v);
dfs(edge[i].v);
sum[now]+=sum[edge[i].v];
max_down[now]=max(max_down[now],sum[edge[i].v]);
}
}
int main(){
int n;
while(~scanf("%d",&n)){
cnt=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(max_down,0,sizeof(max_down));
memset(sum,0,sizeof(sum));
for(int i=1;i<n;i++){
int u,v;
scanf("%d %d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
dfs(1);
int ans=0;
for(int i=1;i<=n;i++){
int x1=n-sum[i],x2=max_down[i];
int x=max(x1,x2);
ans=max(ans,n-x);
}
printf("%d\n",ans);
}
return 0;
}