Balancing Act
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13178   Accepted: 5565

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 <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define dazhi 2147483647
#define bug() printf("!!!!!!!")
#define M 200005
using namespace std;
struct node
{
int from;
int to;
} N[*M];
int n;
int pre[M];
int nedge=;
int son[M];
int vis[M];
int l;
int r;
int ans;
int re;
int t;
void add(int f,int t)
{
nedge++;
N[nedge].to=t;
N[nedge].from=pre[f];
pre[f]=nedge;
}
int getnode(int root)
{
vis[root]=;
son[root]=;
int temp=;
for(int i=pre[root];i;i=N[i].from)
{
int x=N[i].to;
if(vis[x]==)
{
getnode(x);
son[root]+=son[x]+;
temp=max(temp,son[x]+);
}
}
temp=max(temp,n-son[root]-);
if(temp<ans||(temp==ans&&root<re))
{
ans=temp;
re=root;
}
}
int main()
{
scanf("%d",&t);
for(int j=; j<=t; j++)
{
memset(pre,,sizeof(pre));
memset(N,,sizeof(N));
memset(vis,,sizeof(vis));
nedge=;
re=M;
ans=M;
scanf("%d",&n);
for(int i=; i<=n-; i++)
{
scanf("%d %d",&l,&r);
add(l,r);
add(r,l);
}
getnode();
printf("%d %d\n",re,ans);
}
return ;
}

poj 1655 树的重心的更多相关文章

  1. poj 1655 树的重心 && define注意事项

    http://blog.csdn.net/acdreamers/article/details/16905653 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果 ...

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

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

  3. POJ 1655 求树的重心

    POJ 1655 [题目链接]POJ 1655 [题目类型]求树的重心 &题意: 定义平衡数为去掉一个点其最大子树的结点个数,求给定树的最小平衡数和对应要删的点.其实就是求树的重心,找到一个点 ...

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

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

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

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

  6. POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)

    参考网址:http://blog.csdn.net/acdreamers/article/details/16905653   树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...

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

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

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

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

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

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

随机推荐

  1. TW实习日记:第七天

    今天早上,将项目的两个企业微信接口:登录和应用消息发送接口,做了最后的收尾工作,把目前我能解决的问题算是基本都解决了.早上还开了一个会,大意是组长封装了许多组件叫我们使用,在不断的使用中打磨组件的可用 ...

  2. SAP(ABAP) ABAP内部外部数据转换常用function

    文本相关CONVERSION_EXIT_CUNIT_OUTPUT      将内部单位转为单位文本CONVERSION_EXIT_ISOLA_OUTPUT      根据语言代码取文本CONVERSI ...

  3. __autoload 与spl_autoload_register()

    PHP __autoload函数(自动载入类文件)的使用方法 作者: 字体:[增加 减小] 类型:转载 时间:2012-02-04   在使用PHP的OO模式开发系统时,通常大家习惯上将每个类的实现都 ...

  4. Summarize to the Power of Two(map+思维)

    A sequence a1,a2,…,ana1,a2,…,an is called good if, for each element aiai, there exists an element aj ...

  5. Scrum立会报告+燃尽图(十月二十五日总第十六次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...

  6. "助成"招聘网站视频简介

    我们小组为我们的作品录制了一个一分多钟的电梯介绍视频,这是视频连接,我上传到了优酷上:http://v.youku.com/v_show/id_XMzIzMTc1ODc2NA==.html?spm=a ...

  7. c# 捕获一般获取不到的异常

    1.主函数入口加异常事件,代码例如: /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void M ...

  8. 【第二周】scrum站立会议

    1.站立会议:敏捷软件开发方法论Scrum的相关技术之一,是scrum的最佳实践 2.具体形式:每天的同一时间让团队成员面对面站立交流工作进展 3.功能: (1)让团队所有人都相互知道彼此的进展,了解 ...

  9. PHP 生成条形码

    <?php class BarCode128 { const STARTA = 103; const STARTB = 104; const STARTC = 105; const STOP = ...

  10. Node.js系列——(1)安装配置与基本使用

    1.安装 进入下载地址 小编下载的是msi文件,下一步下一步傻瓜式安装. 打印个hello看看: 2.REPL 全称Read Eval Print Loop,即交互式解释器,可以执行读取.执行.打印. ...