Balancing Act
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7986 | Accepted: 3290 |
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
题意:给出一棵树,每个结点都有一个平衡值,一个结点的平衡值是去掉这个结点后形成的森林中最多结点的那棵树的结点数。现在要求出哪个结点的平衡值最小,最小的平衡值为多少。
思路:设以结点i为根的子树的结点数为cnt[i],进行dfs求出每个cnt[i]以及以i为根的树中各个子树的结点数(设最多的结点数为tmp),之后每个结点的平衡值就是max(tmp,n-cnt[i])
AC代码:
#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <ctime> #include <map> #include <algorithm> #define ll __int64 #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) using namespace std; const int maxn = 20005; vector<int>G[maxn]; int n, cnt[maxn], balance[maxn]; void dfs(int u, int pre){ cnt[u] = 1; int tmp = 0; for(int i = 0; i < (int)G[u].size(); i++) { int v = G[u][i]; if(v == pre) continue; dfs(v, u); cnt[u] += cnt[v]; tmp = max(tmp, cnt[v]); } balance[u] = max(tmp, n - cnt[u]); } int main() { int t, a, b; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i = 0; i <= n; i++) G[i].clear(); for(int i = 0; i < n - 1; i++) { scanf("%d%d", &a, &b); G[a].push_back(b); G[b].push_back(a); } memset(cnt, 0, sizeof(cnt)); dfs(1, -1); int x = 1; for(int i = 1; i <= n; i++) if(balance[i] < balance[x]) x = i; printf("%d %d\n", x, balance[x]); } return 0; }