POJ 1655 Balancing Act Tree Center of gravity basic problem

Source: Internet
Author: User

balancing Act
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10347 Accepted: 4285

Description

consider a tree T with N (1 <= n <= 20,000) nodes numbered 1...N. Deleting all node from the tree yields a fores T:a collection of one or more trees. Define the balance of a node to being 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-trees whose member nodes is {5} and {1,2,3,6,7}. The larger of these and trees have 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 the these trees has a nodes, so the balance of node 1 is a.

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

Input

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

Output

For each test case, print a line containing-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 TaskTest Instructions:given a tree, find out the center of gravity of the tree and the number of nodes of the largest subtree of that center of gravity. Center of Gravity:for a tree, if a node is deleted, the tree is divided into one or more subtrees trees. Define the size of the tree : The number of nodes in this tree. If a node is deleted and the size of all sub-trees is no more than 1/2 of the original tree, then the node is called the center of gravity. Note: A tree may have more than one center of gravity. The problem is simply to find the center of gravity of the tree, and the size of the largest subtree of the center of gravity. Dfs once again. Siz[i] The size of the tree with I as the rootSon[i] I the node number of the root of the largest subtree in the subtree of the tree. (as with the tree chain)then Dfs.
1#include <cstdio>2#include <algorithm>3#include <cstring>4 5 using namespacestd;6 7 Const intmaxn=20000+5;8 Const intinf=0x3f3f3f3f;9 Ten structEdge One { A     intTo,next; -}edge[maxn<<1]; -  the intHEAD[MAXN]; - inttot; - intSON[MAXN]; - intSIZ[MAXN]; + intANS[MAXN]; -  + voidInit () A { atmemset (head,-1,sizeof(head)); -tot=1; -memset (ans,0,sizeof(ans)); -memset (son,-1,sizeof(son)); - } -  in voidAddedge (intUintv) - { toedge[tot].to=v; +edge[tot].next=Head[u]; -head[u]=tot++; the } *  $ voidDfsintUintFA)Panax Notoginseng { -siz[u]=1; the      for(intI=head[u];~i;i=edge[i].next) +     { A         intv=edge[i].to; the         if(v==FA) +             Continue; - DFS (v,u); $siz[u]+=Siz[v]; $         if(son[u]==-1|| Siz[v]>Siz[son[u]]) -son[u]=v; -     } the } - Wuyi intSolveintN) the { -Dfs1,-1); Wu  -      for(intI=1; i<=n;i++) AboutAns[i]=max (siz[son[i]],n-siz[i]); $     intId=1; -      for(intI=2; i<=n;i++) -     { -         if(ans[i]<Ans[id]) AId=i; +     } the     returnID; - } $  the intMain () the { the     inttest; thescanf"%d",&test); -      while(test--) in     { the         intN; thescanf"%d",&n); About init (); the  the          for(intI=1; i<n;i++) the         { +             intu,v; -scanf"%d%d",&u,&v); the Addedge (u,v);Bayi Addedge (v,u); the         } the  -         intId=solve (n); -  theprintf"%d%d\n", Id,ans[id]); the     } the  the     return 0; -}
View Code

POJ 1655 Balancing Act Tree Center of gravity basic problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.