Balancing ACT
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 10311 |
|
Accepted: 4261 |
Description
Consider 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.
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 T Hat node.
Sample Input
172 61 21 44 53 73 1
Sample Output
1 2
represents the first contact with a tree DP, the first thought was to use DFS, and use the max_i array to represent the current state of the node its child node of the largest value, and then the sum array to indicate the current state of the node with the number of nodes, but in this case, You have to search through the depth of each node to get the correct results, and the results are presented as tle. Later found that the depth of the search node is 1 of the degree is enough, the result is tle. Finally, see other people's Code, like me, is also the sum array, and a son array, but with Sum[1]-sum[i], in addition to the current node of the child node, the parent node of the node in the branch of the number of nodes. Marvel at this idea, blame oneself have not thought, the thing behind is very simple, before already compared to own child which one node number most, this time direct 22 comparison can.
Code:
#include <iostream> #include <vector> #include <string> #include <cstring> using namespace std; Vector <int> node[20005]; int result,result_num; int used[20005]; int sum[20005];int max_i[20005];int DFS (int i) { used[i]=1; int k; Sum[i]=0;max_i[i]=0;for (K=0;k<node[i].size (); k++) { if (!used[node[i][k]]) { int temp = DFS (Node[i][k]); Sum[i]=sum[i]+temp;if (Temp>max_i[i]) {max_i[i]=temp;}} } Used[i]=0;return sum[i]+1;} int main () { int count,n;cin>>count;while (count--) {Cin>>n;int I,flag;result=20005;memset ( Node,0,sizeof (node)); memset (used,0,sizeof (used)); for (i=1;i<=n-1;i++) {int node1,node2;cin>>node1> >node2;node[node1].push_back (Node2); Node[node2].push_back (Node1);} DFS (1); for (i=1;i<=n;i++) {if (max (Max_i[i],sum[1]-sum[i]) <result) {Result=max (max_i[i],sum[1]-sum[i]); Result_num=i;}} cout<<result_num<< "" <<result<<endl;} return 0; }
POJ 1655:balancing Act