首页 > 其他 > 详细

poj 1655 Balancing Act

时间:2014-03-14 17:46:33      阅读:438      评论:0      收藏:0      [点我收藏+]
Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8048   Accepted: 3322

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: 
bubuko.com,布布扣

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

Source

 
 
 
bubuko.com,布布扣
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<queue>
 6 #include<vector>
 7 #include<algorithm>
 8 using namespace std;
 9 
10 vector<int>Q[20002];
11 int num[20002];
12 int dp[20002][2];
13 //DP[I][0] 统计子节点的最大个数
14 //DP[I][1] 统计 自身及子节点 的总个数
15 bool use[20002];
16 void add(int x,int y)
17 {
18     Q[x].push_back(y);
19     num[x]++;
20 }
21 int Max(int x,int y)
22 {
23     return x>y? x:y;
24 }
25 void dfs(int k)
26 {
27     int i,t,cur=0;
28     use[k]=true;
29     dp[k][1]=1;
30     for(i=0;i<num[k];i++)
31     {
32         t=Q[k][i];
33         if(use[t]==true)continue;
34         dfs(t);
35 
36         if(cur<dp[t][1])
37             cur=dp[t][1];
38         dp[k][1]=dp[k][1]+dp[t][1];
39     }
40     dp[k][0]=cur;
41 }
42 int main()
43 {
44     int T;
45     int i,n,x,y,cur,hxl,tom;
46     scanf("%d",&T);
47     while(T--)
48     {
49         scanf("%d",&n);
50         for(i=0;i<=n;i++)
51         {
52             num[i]=0;
53             Q[i].clear();
54         }
55         memset(dp,0,sizeof(dp));
56         memset(use,false,sizeof(use));
57 
58         for(i=1;i<n;i++)
59         {
60             scanf("%d%d",&x,&y);
61             add(x,y);
62             add(y,x);
63         }
64         dfs(1);
65         hxl=20005;
66         for(i=1;i<=n;i++)
67         {
68             cur=Max(dp[i][0],n-dp[i][1]);
69             if(cur<hxl)
70             {
71                 hxl=cur;
72                 tom=i;
73             }
74         }
75         printf("%d %d\n",tom,hxl);
76     }
77     return 0;
78 }
bubuko.com,布布扣

 

poj 1655 Balancing Act,布布扣,bubuko.com

poj 1655 Balancing Act

原文:http://www.cnblogs.com/tom987690183/p/3599863.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!