POJ 1655: Balancing Act
Balancing Act
Time Limit:1000 MS |
|
Memory Limit:65536 K |
Total Submissions:10311 |
|
Accepted:4261 |
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
It indicates that the first time the node is connected to the tree-like dp, the first thought was to use dfs, and the max_ I array was used to indicate the maximum value of the node's child node under the current condition, then, the sum array indicates the number of nodes contained by the node under the current condition. In this case, you need to perform a deep search on each node to obtain the correct result, the result is indeed submitted by TLE. Later, I found that the Depth Search node level is 1, and the result is still TLE. Finally, we can see other people's code. The same idea as mine is also the sum array, and there is also a son array, however, sum [1]-sum [I] indicates the number of nodes in the branch segment of the Father's Day node except the child node of the current node. I was amazed at this idea and blamed myself for not thinking about it. The next thing is very simple. I have already compared which node has the largest number of my children. This two-to-one comparison is just enough.
Code:
#include
#include
#include
#include
using namespace std; vector
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
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])