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 ;
}
												

最新文章

  1. GraphicsMagick+Im4Java在windows和linux下的配置
  2. tnsnames.ora配置注意(连接新的数据库)
  3. linux下QtCreator无法输入中文的情况
  4. 【leetcode】atoi (hard) ★
  5. Sprint(第五天11.18)
  6. 关于Redis中的数据类型
  7. 漫谈Java虚拟机(JVM)
  8. jQuery 请指出&#39;.bind()&#39;,&#39;.live()&#39;和&#39;.delegate()&#39;的区别
  9. doc2vec 利用gensim 生成文档向量
  10. 1.7.3 Relevance-相关性
  11. SLC和MLC
  12. javascript的null 和undifined
  13. ABP WebApi 加载错误
  14. 使用poi和jfreechart生成excel图表图片
  15. NHibernate从入门到精通系列(3)——第一个NHibernate应用程序
  16. 【leetcode75】Intersection of Two Arrays(数组的交集)
  17. python基础--windows环境下 安装python2和python3
  18. java IO(二)大文件复制
  19. CentOS6.5 安装Storm集群
  20. python 安装opencv 安装pycocotools

热门文章

  1. 【JAVA】两点经纬度直线距离的计算
  2. MySQL5.6 GTID方式,配置主从
  3. 【bzoj1821】[JSOI2010]Group 部落划分 Group
  4. JavaScript Patterns 2.7 Avoiding Implied Typecasting
  5. iOS开发】之CocoaAsyncSocket使用
  6. POI 读取word (word 2003 和 word 2007)(转,好用)
  7. AAC的AudioSpecificConfig细节
  8. 自动生成Makefile的全过程详解
  9. json Date对象在js中的处理办法
  10. frameset的target属性