Balancing Act
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15823 | Accepted: 6703 |
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:
![](https://i-blog.csdnimg.cn/blog_migrate/03339b8159f12781609165773cf436da.jpeg)
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
POJ Monthly--2004.05.15 IOI 2003 sample task
题意:一棵树,定义每个节点的balance值:去掉这点节点后的森林里所有树的最大节点数。求出最小的balance值和其所对应的节点编号
题解:先假定为有向图。
对于某个点,储存其所有子树的节点数。则该节点的balance值为所有子树节点数+ (n - 子树节点数之和 -1)的最大值。
故只需要随意找一个点为根dfs一遍即可!
AC代码
#include <stdio.h>
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
const int maxn = 2e4 + 10, inf = 1 << 29;
int ans1, ans2, vis[maxn];
struct node{
int id, num;
node(int i, int n):id(i), num(n){}
node(){}
}p[maxn];
vector<node> s[maxn];
int dfs(int x){
vis[x] = 1;
int len = s[x].size();
int n = 1;
for(int i = 0; i < len; i++){
int v = s[x][i].id;
if(!vis[v]){
s[x][i].num = dfs(v);
n += s[x][i].num;
}
}
return n;
}
void dfs1(int x, int n){
vis[x] = 1;
int len = s[x].size();
int nmax = 0, sum = 0;
for(int i = 0; i < len; i++){
if(!vis[s[x][i].id])
dfs1(s[x][i].id, n);
sum += s[x][i].num;
nmax = max(s[x][i].num, nmax);
}
nmax = max(nmax, n - sum - 1);
if(nmax < ans1 || (nmax == ans1 && ans2 > x)){
ans1 = nmax;
ans2 = x;
}
}
int main(){
int t, a, b, n;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
for(int i = 1; i <= n; i++){
vis[i] = 0;
s[i].clear();
}
for(int i = 0; i < n - 1; i++){
scanf("%d %d", &a, &b);
s[a].push_back(node(b, 0));
s[b].push_back(node(a, 0));
}
ans1 = inf;
dfs(1);
for(int i = 1; i <= n; i++)
vis[i] = 0;
dfs1(1, n);
printf("%d %d\n", ans2, ans1);
}
return 0;
}