Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14070   Accepted: 5939

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

【思路】
求树的重心
树的中心:删掉这个点后,所形成的连通块最大的最小。
dp[i]为删掉这个点后最大的连通块的值。
edge数组开小了runtime erroe 
【code】
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,x,y,G,sumedge,t;
int head[],size[],dad[],dp[];
struct Edge
{
int x,y,nxt;
Edge(int x=,int y=,int nxt=):x(x),y(y),nxt(nxt){}
}edge[];
void add(int x,int y)
{
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
}
void init()
{
sumedge=;
memset(head,,sizeof(head));
memset(size,,sizeof(size));
memset(dad,,sizeof(dad));
memset(dp,,sizeof(dp));
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
}
void dfs(int x)
{
size[x]=;
for(int i=head[x];i;i=edge[i].nxt)
{
int v=edge[i].y;
if(dad[x]!=v)
{
dad[v]=x;
dfs(v);
size[x]+=size[v];
dp[x]=max(dp[x],size[v]);//最大的孩子
}
}
dp[x]=max(dp[x],n-size[x]);//不是子树的那一堆
}
void print()
{
int ans=0x7fffff;
for(int i=;i<=n;i++)
if(dp[i]<ans)ans=dp[i],G=i;
printf("%d %d\n",G,ans);
}
int main()
{
scanf("%d",&t);
while(t--)
{
init();
dfs();
print();
}
return ;
}
 

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. 『Balancing Act 树的重心』

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

  3. POJ1655 Balancing Act(树的重心)

    题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP ...

  4. poj-1655 Balancing Act(树的重心+树形dp)

    题目链接: Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11845   Accepted: 4 ...

  5. PKU 1655 Balancing Act(树+树的重心)

    #include<cstdio> #include<cstring> #include<algorithm> #define maxn 20005 using na ...

  6. POJ 1655 - Balancing Act 树型DP

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

  7. poj1655 Balancing Act 找树的重心

    http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  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 ...

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

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

随机推荐

  1. 虚拟机centos 里tomcat的端口映射到主机 Windows里面

  2. python 获取时间 存入文件

    1读文件: file_path_name = '/home/robot/bzrobot_ws/src/bzrobot/bzrobot_comm/led_show_data/'+file_name+'. ...

  3. PLsql/Oracle数据库中没有scott账户,如何创建并解锁

    当然首先要装好Oracle 11g 然后还要有sqlplus,这个在Oracle11g的时候应该都会配上的 进入正题,如果oracle/plsql没scott账户,如何创建 先找到Oracle安装目录 ...

  4. springcloud 学习笔记

    ---恢复内容开始--- 1. pom配置 1.1 引入spring boot 依赖 <parent> <groupId>org.springframework.boot< ...

  5. 前端编程提高之旅(十)----表单验证插件与cookie插件

        实际项目开发中与用户交互的常见手法就是採用表单的形式.取得用户注冊.登录等信息.而当用户注冊或登录后又须要记住用户的登录状态.这就涉及到经常使用的两个操作:表单验证与cookie增删查找.   ...

  6. CentOS安装Openfire服务

    原文::http://xiao987334176.blog.51cto.com/2202382/979677 系统是全新新安装的系统.版本号是Centos 5.6 x86 同步北京时间 # ntpda ...

  7. JQUERY多选框,单选框,检查选中的值

    var str=""; $(":checkbox:checked").each(function(){ if($(this).attr("checke ...

  8. Development of Intel chipsets interconnection

    http://en.wikipedia.org/wiki/Chipset Chipset From Wikipedia, the free encyclopedia     A chipset is ...

  9. HashTable源代码剖析

    <span style="font-size:14px;font-weight: normal;">public class Hashtable<K,V> ...

  10. Beijing Bus

    Search: http://bjgj.aibang.com:8899 https://github.com/sp-chenyang/bus https://github.com/leavind/Co ...