Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17497   Accepted: 7398

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

 
 
 
 
题意就是找树的重心,然后通过树的重心的概念,找到树的重心,删掉之后子树是最平衡的。直接贴的模板。
 
代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n,father;
int siz[maxn];//siz保存每个节点的子树大小
bool vist[maxn];
int point=inf,minsum=-;//minsum表示切掉重心后最大连通块的大小
vector<int>G[maxn]; void DFS(int u,int x)//遍历到节点x,x的父亲是u
{
siz[x]=;
bool flag=true;
for(int i=;i<G[x].size();i++){
int v=G[x][i];
if(!vist[v]){
vist[v]=true;
DFS(x,v);//访问子节点。
siz[x]+=siz[v];//回溯计算本节点的siz
if(siz[v]>n/) flag=false;//判断节点x是不是重心。
}
}
if(n-siz[x]>n/) flag=false;//判断节点x是不是重心。
if(flag&&x<point) point=x,father=u;//这里写x<point是因为本题中要求节点编号最小的重心。
} void init()
{
memset(vist,false,sizeof(vist));
memset(siz,,sizeof(siz));
minsum=-;
point=inf;
for(int i=;i<maxn;i++) G[i].clear();
} int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
init();
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
vist[]=;
DFS(-,);//任意选取节点作为根,根节点的父亲是-1。
for(int i=;i<G[point].size();i++)
if(G[point][i]==father) minsum=max(minsum,n-siz[point]);
else minsum=max(minsum,siz[G[point][i]]);
printf("%d %d\n",point,minsum);
}
return ;
}

POJ 1655.Balancing Act-树的重心(DFS) 模板(vector存图)的更多相关文章

  1. POJ 1655 Balancing Act 树的重心

    Balancing Act   Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...

  2. POJ 1655 - Balancing Act 树型DP

    这题和POJ 3107 - Godfather异曲同工...http://blog.csdn.net/kk303/article/details/9387251 Program: #include&l ...

  3. POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)

    关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...

  4. poj 1655 Balancing Act 求树的重心【树形dp】

    poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...

  5. POJ 1655 Balancing Act【树的重心模板题】

    传送门:http://poj.org/problem?id=1655 题意:有T组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...

  6. 『Balancing Act 树的重心』

    树的重心 我们先来认识一下树的重心. 树的重心也叫树的质心.找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡. 根据树的重心的定义,我们可 ...

  7. POJ 1655 - Balancing Act - [DFS][树的重心]

    链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tre ...

  8. POJ 1655 Balancing Act【树的重心】

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14251   Accepted: 6027 De ...

  9. POJ 1655.Balancing Act 树形dp 树的重心

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14550   Accepted: 6173 De ...

随机推荐

  1. Rsync+inotify自动同步数据

    一.简介 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足. 首先,rsync在同步数据时,需要扫描所有文件后进行比对,进行差量传 ...

  2. 精通BIRT:Eclipse商务智能报表工具开发实践指南

    http://blog.csdn.net/birtbird/article/details/8935520 [置顶] 精通BIRT:Eclipse商务智能报表工具开发实践指南 分类: BIRT 201 ...

  3. 51Nod 1062 序列中最大的数 | 简单DP

    #include "iostream" #include "cstdio" using namespace std; #define LL long long ...

  4. pythonweb框架

    https://www.cnblogs.com/sss4/p/8097653.html

  5. Linux Shell下执行sqlplus

    转载自: http://www.cnblogs.com/include/archive/2011/12/30/2307889.html 以下方法解决了在linux下自动的删除创建用户 sqlplus ...

  6. Codeforces Round #411 (Div. 2) A-F

    比赛时候切了A-E,fst了A Standings第一页只有三个人挂了A题,而我就是其中之一,真™开心啊蛤蛤蛤 A. Fake NP time limit per test 1 second memo ...

  7. 51nod 1106 质数检测——Mr判素数

    质数检测一般都是根号n的写法 当然Mr判素数的方法可以实现log的复杂度2333 Mr判素数的话 我们根据费马小定理只要P是素数 那么另一个素数x 满足 x^P-1≡1(mod P) 同时 x^2%P ...

  8. js常用模板引擎

    baiduTemplate(百度).artTemplate(腾讯).juicer(淘宝).xtemplate.doT.Jade 1.Handlebars 是 JavaScript 一个语义模板库,通过 ...

  9. centos rar 文件打开办法

    http://hi.baidu.com/nmxiaoxin/item/7642a139918a95677d034b6a Centos下解压rar.zip文件的方法 ============zip文件的 ...

  10. 64_g5

    golang-github-kr-text-devel-0-0.11.git6807e77.f..> 11-Feb-2017 07:48 14250 golang-github-kr-text- ...