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个节点的数,让你求出树的重心以及以重心为根的子树中最大的子树节点个数。

思路:

紧扣树的重心的定义:

定义为:在树中招到一个节点,其所有子树中最大的子树节点个数最小,那么这个点就是树的重心,删除重心后,生成的多颗树尽可能的平衡。

性质:

1、树中所有点到某个点的距离的sum和,到重心的距离sum和是最小的。如果有两个重心,那么他们的距离sum和是一样的、

2、把两棵树通过一个边连接得到一颗新的树,那么新的树的重心在原来两个树的重心的路径上。

3、把一个树添加或删除一个叶子节点,那么它的重心最多移动一个边的距离。

我们根据“所有子树中最大的子树节点个数最小”,这个性质,在dfs整颗树的时候,用cntson[i] 数组记录第i个节点为根的子树节点个数

用在dfs函数用,用cnt记录 当前节点的最大子树节点个数。

那么直需要维护cnt中的最小值即可得到答案。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
std::vector<int> son[maxn];
int t;
int n;
int ans1;
int ans2;
int cntson[maxn];
void dfs(int x,int pre)
{
cntson[x]=1;
int cnt=0;
// for(auto y:son[x])
for(int i=0;i<sz(son[x]);++i)
{
int y=son[x][i];
if(y!=pre)
{
dfs(y,x);
cntson[x]+=cntson[y];
cnt=max(cnt,cntson[y]);
}
}
cnt=max(cnt,n-cntson[x]);
if(cnt<ans2||(cnt==ans2&&x<ans1))
{
ans2=cnt;
ans1=x;
}
}
void init()
{
repd(i,1,n)
{
son[i].clear();
}
ans2=inf;
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
repd(i,2,n)
{
int u,v;
scanf("%d %d",&u,&v);
son[v].push_back(u);
son[u].push_back(v);
}
dfs(1,1);
printf("%d %d\n",ans1,ans2 );
}
return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

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

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

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

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

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

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

    求树的重心,直接当模板吧.先看POJ题目就知道重心什么意思了... 重心:删除该节点后最大连通块的节点数目最小 #include<cstdio> #include<cstring&g ...

  4. poj 1655 树的重心

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

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

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

  6. POJ 1655 Balancing Act(求树的重心)

    Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any nod ...

  7. I - Balancing Act POJ - 1655

    Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...

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

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

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

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

随机推荐

  1. linux常用命令(14)which命令

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索:    which  查看可执行文件的位置.   whereis 查看文件的位置.    locate   配合数 ...

  2. RN 图片处理 resizeMode

    Image组件必须在样式中声明图片的宽和高.如果没有声明,则图片将不会被呈现在界面上.    我们一般将Image定义的宽和高乘以当前运行环境的像素密度称为Image的实际宽高. 当Image的实际宽 ...

  3. Unity中的全局坐标系和局部坐标系

      全局坐标系 描述游戏场景内所有物体位置和方向的基准,也称为世界坐标系.在Unity场景中创建的物体都是以全局坐标系中的坐标原点(0,0,0)来确定各自的位置的. 局部坐标系 每个物体都有其独立的坐 ...

  4. PJzhang:从csdn到pipal密码分析工具

    猫宁!!! 偶然看到一篇短文,是对2011年csdn泄露的约643万数据的数据汇总分析,这里做个简要总结 .   一.单一密码使用5000人以上的,14个,当时密码中有两个命名方式很有趣,dearbo ...

  5. mysql数据的备份

    一.备份方式 1.备份:逻辑备份(mysqldump,mydumper).物理备份(xtrabackup.tar.cp.rsync)    2.冗余:主备模式.数据库集群 二.备份对象 1.数据(库. ...

  6. 修改iframe内元素的样式

      $('iframe').load(function () { var x = document.getElementsByTagName('iframe')[0]; var y = (x.cont ...

  7. PHP7 开启Zend Opcache

    PHP7 开启Zend Opcache 作为PHP这10年来最大的版本与性能升级,PHP7在多次的测试中都表现出很夸张的性能提升,然而,为了让它能发挥出最大的性能,需要手动开启PHP自带的opcach ...

  8. Chcp,Chdir(Cd),Chkdsk和Chkntfs

    Chdir(缩写为cd)(全称猜测是change drive):显示或更改当前目录的名称; 注c:a/b\c/d 表示C盘下的a的b的c的d,目录可用' / '(正斜)或 ' \ '(反斜),参数只能 ...

  9. 面向服务架构之RPC原理与实例

    1.RPC概述 RPC(Remote Procedure Call)即远程过程调用,允许一台计算机调用另一台计算机上的程序得到结果,而代码中不需要做额外的编程,就像在本地调用一样.主要是为了应对当前互 ...

  10. Java白皮书学习笔记+Head First Java--用于自我复习 基础知识篇

    本笔记是摘与Hava白皮书上面的内容,用来给自己做提醒的,因此大概并不适合Java的学习者作为笔记参考使用. 以我的水平现在还看不懂这个... 一.基础知识篇 1.常量 final关键字指示常量,只能 ...