Balancing Act(树的重心)

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

题意:给你一颗树,去掉一个结点,使得最大的连通块的最小,输出去掉的结点和最大连通块的大小。

题解:树的重心模板题,变无根树为有根树,跑dfs计算根为i的子树的结点个数f[i]并记录结点i的儿子;然后遍历一遍,最大连通块大小就应该是所有儿子中f的最大值,再与n-f[i]比较大小。

//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node << 1
#define rson mid + 1, r, node << 1 | 1
const int INF  =  0x3f3f3f3f;
const int O    =  1e5;
const int mod  =  1e9 + 7;
const int maxn =  2e4+5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;

//变无根树为有根树
vector<int>ve[maxn]; //记录无根树
vector<int>son[maxn]; //son[i]记录结点i的儿子
int f[maxn]; //f[i]表示根为i的子树的结点个数
int dfs(int u, int fa){
    for(int i=0; i<ve[u].size(); i++){
        int v = ve[u][i];
        if(v == fa) continue;
        son[u].push_back(v);
        f[u] += dfs(v, u);
    }
    return f[u] += 1;
}

int main(){
    int T; scanf("%d", &T);
    while( T --){
        int n; scanf("%d", &n);
        for(int i=1; i<=n; i++) { ve[i].clear(); son[i].clear(); f[i] = 0; }
        for(int i=0; i<n-1; i++) {
            int u, v; scanf("%d%d", &u, &v);
            ve[u].push_back(v);
            ve[v].push_back(u);
        }
        dfs(1, -1);
        int p = 1, num = INF; //去掉的结点p,与最大连通块的大小
        for(int i=1; i<=n; i++){
            int maxx = n - f[i];
            for(int j=0; j<son[i].size(); j++) maxx = max(maxx, f[son[i][j]]);
            if(num > maxx) { num = maxx; p = i; }
        }
        printf("%d %d\n", p, num);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mannix_Y/article/details/85037440