Balancing Act
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:7412 |
|
Accepted:2994 |
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 |
For a small question about poj MAN 8: 1741, refer to my blog.
# Include <iostream> # include <string. h> # include <stdio. h> using namespace STD; struct node1 // point structure {int sum; // sum point of the stored subtree int Maxs; // maximum size of the son node. See my most recent man's eight questions} points [20010]; struct node2 // Edge Structure {int to; // end node2 * Next;} edge [40010], * Head [20010]; int POS [20010], pmaxs [20010]; int CNT, ANS, PTR; void Adde (int f, int S) // Add edge {edge [CNT]. next = head [f]; edge [CNT]. to = s; head [f] = & edge [CNT ++];} int Ma (int A, int B) {return A> B? A: B;} void DFS (INT FA, int son) // uses s as the root node to traverse the tree. Sum points of the computing tree and the maximum size of the subtree {points [son]. maxs = 0; points [son]. sum = 1; node2 * P = head [son]; while (P! = NULL) {If (p->! = FA) {DFS (son, p-> to); points [son]. maxs = MA (points [son]. maxs, points [p-> to]. sum); points [son]. sum + = points [p-> to]. SUM;} p = p-> next;} POS [PTR] = Son; // Save the calculated result to pmaxs [PTR ++] = points [son] in the array. maxs;} void solve (INT s) {int I, Mins, temp; PTR = 0; DFS (0, S); temp = points [s]. SUM; mins = 0x3f3f3f; for (I = 0; I <PTR; I ++) {pmaxs [I] = MA (pmaxs [I], temp-points [POS [I]. sum); If (pmaxs [I] <mins) // find the center of gravity mins = pmaxs [I], ANS = I; else if (pmaxs [I] = mins & Pos [I] <POS [ANS]) ans = I ;}} int main () {int T, I, n, a, B; scanf ("% d", & T); While (t --) {scanf ("% d", & N); memset (Head, 0, sizeof head); CNT = 0; for (I = 1; I <n; I ++) {scanf ("% d", & A, & B ); ADDE (a, B); Adde (B, A);} solve (1); printf ("% d \ n", POS [ANS], pmaxs [ANS]);} return 0 ;}