| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 10229 |
|
Accepted: 4221 |
Descriptionconsider a tree T with N (1 <= n <= 20,000) nodes numbered 1...N. Deleting all node from the tree yields A forest: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. Inputthe 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. Outputfor, print a line containing, integers, the number of the node with minimum balance and the Balanc E of that node. Sample Input
172 61 21 44 53 73 1
Sample Output
1 2
The crux of the puzzle: the center of gravity of the tree, and the maximum number of sub-tree nodes that are removed. The center of gravity of the tree is to find a node in the tree, so that the number of nodes formed after the deletion of the node is as balanced as possible (that is, the largest sub-tree is the smallest borrowed points). Method: DFS
CODE:
#include <iostream>#include<cstdio>#include<cstring>#defineREP (i, S, N) for (int i = s; i <= n; i + +)#defineRep_ (i, S, N) for (int i = n; i >= s; i-)#defineMax_n 20000 + 10using namespacestd;intN, u, v;structnode1{intV, next; } e[max_n <<1];structnode2{intsum, CNT;} N[max_n];intHead[max_n], top =0;voidAddintUintV) {e[++ top].v = v; E[top].next = Head[u]; Head[u] =top;}intDFS (intXintLast ) {N[x].sum=1; n[x].cnt =0; for(inti = head[x]; I i =E[i].next) { if(E[I].V! =Last ) { intt =DFS (E[I].V, x); N[x].sum+ = t; n[x].cnt =Max (n[x].cnt, T); } } returnn[x].sum;}intMain () {intT scanf"%d", &T); while(T--) {Top=0; Memset (Head,0,sizeof(head)); scanf ("%d", &N); REP (i,1N1) {scanf ("%d%d", &u, &v); Add (U, v); Add (V, u); } DFS (1,0); intAns =99999999, num =0; REP (i,1, N) {n[i].cnt= Max (n[i].cnt, N-n[i].sum); if(Ans >n[i].cnt) {ans= n[i].cnt; num =i; }} printf ("%d%d\n", num, ans); } return 0;}
POJ 1655 Balancing Act