poj-1655 - The center of gravity of the tree

Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15526   Accepted: 6575

Description

Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the largest tree in the forest T created by deleting that node from T. 
For example, consider the tree: 

Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two. 

For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number. 

Input

The first line of input contains a single integer t (1 <= t <= 20), the number of test cases. The first line of each test case contains an integer N (1 <= N <= 20,000), the number of congruence. The next N-1 lines each contains two space-separated node numbers that are the endpoints of an edge in the tree. No edge will be listed twice, and all edges will be listed.

Output

For each test case, print a line containing two integers, the number of the node with minimum balance and the balance of that node.

Sample Input

1
7
2 6
1 2
1 4
4 5
3 7
3 1

Sample Output

1 2 

Find the balance point of a given tree, that is, the largest subtree of this point. The number of nodes in the forest is as small as possible. Think of the tree dp, but the largest subtree of the child node may come from
the father, but the number of nodes in the tree from the father can be calculated by calculating the number of nodes whose current node is the root and then subtracting it from N. Obviously, it can be recursive Time to figure it out.
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 using namespace std;
 6 vector<int>g[20010];
 7 int f[20010],num[20010],n;
 8 void dfs(int u,int fa){
 9     f[u]=0,num[u]=1;
10     for(int i=0;i<g[u].size();++i){
11         if(g[u][i]==fa) continue;
12         dfs(g[u][i],u);
13         f[u]=max(f[u],num[g[u][i]]);
14         num[u]+=num[g[u][i]];
15     }
16     f[u]=max(f[u],n-num[u]);
17 }
18 int main()
19 {
20     int t,m,i,j;
21     int u,v;
22     cin>>t;
23     while(t--){
24         cin>>n;
25         for(i=1;i<=n;++i) g[i].clear();
26         for(i=1;i<n;++i){
27             scanf("%d%d",&u,&v);
28             g[u].push_back(v);
29             g[v].push_back(u);
30         }
31         dfs(1,0);
32         int ans1=0,ans2=999999;
33         for(i=1;i<=n;++i){
34             if(f[i]<ans2){
35                 ans1=i;
36                 ans2=f[i];
37             }
38         }
39         cout<<ans1<<' '<<ans2<<endl;
40     }
41     return 0;
42 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324395232&siteId=291194637