POJ 1655 Balancing Act(求树的重心)

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.
 
题目大意:给一棵树,问删掉哪个结点后,剩下的树的最大结点数最小。
思路:DFS一次即可求出所有点的子树大小size,顺便算出每个点最大的子树maxSize。那么对于每个点删掉之后,剩下的树的最大结点数就是max(maxSize, n - size),前面就是它的所有子树的最大size,后面就是删掉这个点后,它父亲所在的树的大小。
在研究树的分治之前先来补一条水题。。。这也算DP- -?
 
代码(47MS):
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int MAXN = 20010;
 8 const int MAXE = 40010;
 9 const int INF = 0x7fff7fff;
10 
11 int head[MAXN], size[MAXN], maxSize[MAXN], f[MAXN];
12 int to[MAXE], next[MAXE];
13 int n, ecnt;
14 
15 void init() {
16     memset(head, -1, sizeof(head));
17     ecnt = 0;
18 }
19 
20 void add_edge(int u, int v) {
21     to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
22     to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
23 }
24 
25 void dfs(int u) {
26     maxSize[u] = 0;
27     size[u] = 1;
28     for(int p = head[u]; ~p; p = next[p]) {
29         int &v = to[p];
30         if(v == f[u]) continue;
31         f[v] = u;
32         dfs(v);
33         size[u] += size[v];
34         maxSize[u] = max(maxSize[u], size[v]);
35     }
36 }
37 
38 int main() {
39     int T;
40     scanf("%d", &T);
41     while(T--) {
42         init();
43         scanf("%d", &n);
44         int u, v;
45         for(int i = 1; i < n; ++i) {
46             scanf("%d%d", &u, &v);
47             add_edge(u, v);
48         }
49         dfs(1);
50         int pos, maxd = INF;
51         for(int i = 1; i <= n; ++i) {
52             if(max(maxSize[i], n - size[i]) < maxd) {
53                 pos = i;
54                 maxd = max(maxSize[i], n - size[i]);
55             }
56         }
57         printf("%d %d\n", pos, maxd);
58     }
59 }
View Code

 

posted @ 2013-11-05 16:49  Oyking  阅读(344)  评论(0编辑  收藏  举报