码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1655 Balancing Act 树的重心 基础题

时间:2015-06-20 17:00:40      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10347   Accepted: 4285

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

1
7
2 6
1 2
1 4
4 5
3 7
3 1

Sample Output

1 2

Source

 
 
 
 
题意:
给出一棵树,求出树的重心和该重心的最大的子树的节点个数。
 
重心:
对于一棵树,若删除某一个节点,这棵树会分成一棵或者多棵子树。
定义树的大小:这棵树的节点个数。
若一个节点删除后分成的所有子树的大小都不超过原树的1/2,则该节点称之为重心。
 
注意:一棵树的重心可能不止一个。
 
这道题就是简单的求树的重心,和该重心的最大的子树的大小。
 
dfs一遍即可。
 
siz[i]以i为根的树的大小
son[i] i的子树中大小最大的子树的根的节点编号。
(和树链剖分一样)
 
然后dfs。
 
 
技术分享
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 const int maxn=20000+5;
 8 const int inf=0x3f3f3f3f;
 9 
10 struct Edge
11 {
12     int to,next;
13 }edge[maxn<<1];
14 
15 int head[maxn];
16 int tot;
17 int son[maxn];
18 int siz[maxn];
19 int ans[maxn];
20 
21 void init()
22 {
23     memset(head,-1,sizeof(head));
24     tot=1;
25     memset(ans,0,sizeof(ans));
26     memset(son,-1,sizeof(son));
27 }
28 
29 void addedge(int u,int v)
30 {
31     edge[tot].to=v;
32     edge[tot].next=head[u];
33     head[u]=tot++;
34 }
35 
36 void dfs(int u,int fa)
37 {
38     siz[u]=1;
39     for(int i=head[u];~i;i=edge[i].next)
40     {
41         int v=edge[i].to;
42         if(v==fa)
43             continue;
44         dfs(v,u);
45         siz[u]+=siz[v];
46         if(son[u]==-1||siz[v]>siz[son[u]])
47             son[u]=v;
48     }
49 }
50 
51 int solve(int n)
52 {
53     dfs(1,-1);
54 
55     for(int i=1;i<=n;i++)
56         ans[i]=max(siz[son[i]],n-siz[i]);
57     int id=1;
58     for(int i=2;i<=n;i++)
59     {
60         if(ans[i]<ans[id])
61             id=i;
62     }
63     return id;
64 }
65 
66 int main()
67 {
68     int test;
69     scanf("%d",&test);
70     while(test--)
71     {
72         int n;
73         scanf("%d",&n);
74         init();
75 
76         for(int i=1;i<n;i++)
77         {
78             int u,v;
79             scanf("%d%d",&u,&v);
80             addedge(u,v);
81             addedge(v,u);
82         }
83 
84         int id=solve(n);
85 
86         printf("%d %d\n",id,ans[id]);
87     }
88 
89     return 0;
90 }
View Code

 

 
 
 
 
 
 
 
 
 
 
 
 

POJ 1655 Balancing Act 树的重心 基础题

标签:

原文地址:http://www.cnblogs.com/-maybe/p/4590674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!