Balancing Act
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9199 | Accepted: 3826 |
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.
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
1 7 2 6 1 2 1 4 4 5 3 7 3 1
Sample Output
1 2
解决方案:dp[i]表示balance,dpson[i]表示包括这个节点及其所有儿子的节点的个数。
dp[i]=max(dpson[son1...n],n-dpson[i]);n为总节点个数。另外注意的是输出时并没有分配父节点与儿子节点是哪个。
code:#include <iostream> #include<cstdio> #include<vector> #include<cstring> using namespace std; const int maxn=20003; int in[maxn],dp[maxn],dpson[maxn]; bool vis[maxn]; vector<int>G[2*maxn]; int n; void dfs(int u) { if(vis[u]) return; vis[u]=true; int len=G[u].size(); for(int i=0; i<len; i++) { int v=G[u][i]; if(v!=u&&!vis[v]) { dfs(v); dp[u]=max(dp[u],dpson[v]); dpson[u]+=dpson[v]; } } dpson[u]+=1; dp[u]=max(dp[u],n-dpson[u]); } int main() { int t; // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); scanf("%d",&t); while(t--) { scanf("%d",&n); memset(in,0,sizeof(in)); memset(dp,0,sizeof(dp)); memset(dpson,0,sizeof(dpson)); memset(vis,false,sizeof(vis)); for(int i=1;i<=n;i++)G[i].clear(); for(int i=0; i<n-1; i++) { int from,to; scanf("%d%d",&from,&to); G[from].push_back(to); G[to].push_back(from); } dfs(1); int pre=dp[1],node=1; for(int i=1; i<=n; i++) { if(dp[i]<pre) { node=i; pre=dp[i]; } } printf("%d %d\n",node,dp[node]); } return 0; }