Poj 1655 Balancing Act (center of gravity, tree dp), poj1655balancing
Balancing Act
Time Limit:1000 MS |
|
Memory Limit:65536 K |
Total Submissions:10375 |
|
Accepted:4296 |
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 172 61 21 44 53 73 1 Sample Output 1 2 Source POJ Monthly -- 2004.05.15 IOI 2003 sample task |
[Submit] [Go Back] [Status] [Discuss]
The center of gravity of a tree is defined as: finding a vertex, and the maximum number of subtree nodes in all its Subtrees is the least. Then this vertex is the center of gravity of the tree.
After the heart, the multiple trees generated are as balanced as possible. in fact, the center of gravity of the tree plays an important role in tree point division, which can avoid the extreme complexity of N ^ 2 (starting from the end of the degraded chain) and ensure the complexity of NlogN, the center of gravity of the tree can be solved using tree dp. set siz [x] to the number of subtree nodes with x as the root, and f [x] to the largest siz [v] In subtree v with x as the root. Note that x's father cannot collect statistics in this case. We can use the total number of nodes minus siz [x] to get the size of the subtree rooted in x's father.
#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>#include<cstring>using namespace std;const int maxn = 2e4 + 10;const int inf = 1e8;int siz[maxn],f[maxn],root,tot,res;vector<int>G[maxn];void AddEdge(int u,int v){G[u].push_back(v);G[v].push_back(u);}void getroot(int u,int fa){siz[u] = 1;f[u] = 0;for(int i = 0; i < G[u].size(); ++i) {int v = G[u][i];if(v==fa)continue;getroot(v,u);siz[u] += siz[v];f[u] = max(f[u],siz[v]);}f[u] = max(f[u],tot - siz[u]);int t = f[u];if(f[u] < f[root]) root = u,res = t;if(u < root && f[u] == f[root]) root = u,res = t;}int getsize(int u,int fa){int ans = 1;for(int i = 0; i < G[u].size(); i++) {int v = G[u][i];if(v==fa)continue;ans += getsize(v,u);}return ans;}int main(int argc, char const *argv[]){int T;scanf("%d",&T);while(T--) {int n;scanf("%d",&n);tot = n;f[root = 0] = inf;for(int i = 1; i <= n; i++) {G[i].clear();}for(int i = 1; i < n; i++) {int u,v;scanf("%d%d",&u,&v);AddEdge(u,v);}getroot(1,-1);printf("%d %d\n", root,res);}return 0;}