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

题意就是给你一棵无根树,让你找到一个点,去掉这个点之后使得剩下的子树的最大节点数最小;

思路,就是求树的重心,下面先给出树的重心的定义:对于一棵n个节点的无根树,找到一个点使得把树变成一棵以该节点为根的有根树,这时的最大子树的节点数最小。
定义sizes[i]表示i的最大子树的节点数,定义dp[i]为以i为根的最大子树的节点数。然后递归求解。
 #include <iostream>
#include <cstdio>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=1e6+; int dp[maxn];
int sizes[maxn];
int ans,n,sum;
vector<int> v[maxn];//二维矩阵存图
void dfs(int x,int fa)
{
sizes[x]=;
int maxx=;
for(int i=;i<v[x].size();i++)
{
int y=v[x][i];
if(y!=fa)
{
dfs(y,x);
sizes[x]+=sizes[y];
maxx=max(maxx,sizes[y]);
} }
dp[x]=max(maxx,n-sizes[x]);
if(sum>dp[x])
{
ans=x;
sum=dp[x];
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
sum=0x3f3f3f3f;
scanf("%d",&n);
memset(dp,,sizeof(dp));
memset(sizes,,sizeof(sizes));
for(int i=;i<=n;i++)v[i].clear();
for(int i=;i<n;i++)
{
int L,K;
scanf("%d%d",&L,&K);
v[K].push_back(L);
v[L].push_back(K);
}
dfs(,-);
printf("%d %d\n",ans,dp[ans]);
} return ;
}
												

POJ-1655 Balancing Act(树的重心)的更多相关文章

  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【树的重心】

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

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

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

  7. poj 1655 Balancing Act(找树的重心)

    Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或 ...

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

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

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

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

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

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

随机推荐

  1. layoutSubviews, setNeedsLayout, layoutIfNeeded

    layoutSubviews总结 ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size- (void)sizeToFit——————- - (voi ...

  2. Project Euler:Problem 42 Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

  3. JSP开发学习参考文章

    配置JDK和Tomcat环境变量 http://blog.csdn.net/lijiazhi1987/article/details/2742181 eclipse maven plugin 插件安装 ...

  4. CSS经典布局之弹性布局

    当我们在浏览浏览器的时候,经常会放大/缩小浏览器的显示比例,或者在不同的设备上.所处的分辨率也不尽同样. 因此.我们须要学习一个新的知识:弹性盒模型. 弹性盒模型 实现项目对齐,方向,排序(即使项目大 ...

  5. Oracle 用户管理(二)

    1    给某人赋予"系统权限"     SQL> grant connect to aobama with admin option     意思是将admin的连接数据库 ...

  6. 解决myeclipse中struts2 bug问题包的替换问题

    由于struts2的bug问题,手工替换还是比較麻烦.但即便是最新的myeclipse2014也没有替换最新的struts2包,研究了一天,最终找到了解决的方法.下面就解决方法与大家分享一下. 1.在 ...

  7. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  8. The bytes/str dichotomy in Python 3

    The bytes/str dichotomy in Python 3 - Eli Bendersky's website https://eli.thegreenplace.net/2012/01/ ...

  9. Office 修改语言

  10. [App Store Connect帮助]三、管理 App 和版本(2.4)输入 App 信息:提供加密出口合规证明文稿

    上传至 App Store Connect 的 App 被上传至位于美国的 Apple 服务器.如果您提交 App 的目的是为了在 App Store 上分发您的 App 或通过美国或加拿大的境外 T ...