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.
 
题目大意:给一棵树,问删掉哪个结点后,剩下的树的最大结点数最小。
思路:DFS一次即可求出所有点的子树大小size,顺便算出每个点最大的子树maxSize。那么对于每个点删掉之后,剩下的树的最大结点数就是max(maxSize, n - size),前面就是它的所有子树的最大size,后面就是删掉这个点后,它父亲所在的树的大小。
在研究树的分治之前先来补一条水题。。。这也算DP- -?
 
代码(47MS):
 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = ;
const int MAXE = ;
const int INF = 0x7fff7fff; int head[MAXN], size[MAXN], maxSize[MAXN], f[MAXN];
int to[MAXE], next[MAXE];
int n, ecnt; void init() {
memset(head, -, sizeof(head));
ecnt = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
} void dfs(int u) {
maxSize[u] = ;
size[u] = ;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == f[u]) continue;
f[v] = u;
dfs(v);
size[u] += size[v];
maxSize[u] = max(maxSize[u], size[v]);
}
} int main() {
int T;
scanf("%d", &T);
while(T--) {
init();
scanf("%d", &n);
int u, v;
for(int i = ; i < n; ++i) {
scanf("%d%d", &u, &v);
add_edge(u, v);
}
dfs();
int pos, maxd = INF;
for(int i = ; i <= n; ++i) {
if(max(maxSize[i], n - size[i]) < maxd) {
pos = i;
maxd = max(maxSize[i], n - size[i]);
}
}
printf("%d %d\n", pos, maxd);
}
}

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

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

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

  2. POJ 1655 Balancing Act(求树的重心--树形DP)

    题意:求树的重心的编号以及重心删除后得到的最大子树的节点个数size,假设size同样就选取编号最小的. 思路:随便选一个点把无根图转化成有根图.dfs一遍就可以dp出答案 //1348K 125MS ...

  3. POJ 1655 Balancing Act (求树的重心)【树形DP】(经典)

    <题目链接> 题目大意:给你一棵树,任意去除某一个点后,树被分成了几个联通块,则该点的平衡值为所有分成的连通块中,点数最大的那个,问你:该树所有点中,平衡值最小的那个点是什么? 解题分析: ...

  4. POJ 1655 Balancing Act (树的重心,常规)

    题意:求树的重心,若有多个重心,则输出编号较小者,及其子树中节点最多的数量. 思路: 树的重心:指的是一个点v,在删除点v后,其子树的节点数分别为:u1,u2....,设max(u)为其中的最大值,点 ...

  5. POJ 1655 Balancing Act【树的重心】

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

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

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

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

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

  8. POJ 1655 Balancing Act ( 树的重心板子题,链式前向星建图)

    题意: 给你一个由n个节点n-1条边构成的一棵树,你需要输出树的重心是那个节点,以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...

  9. POJ 1655 Balancing Act 焦点树

    标题效果:鉴于一棵树.除去一个点之后,这棵树将成为一些中国联通的块.之后该点通过寻求取消最低形成块的最大数目. 思维:树DP思维.通过为每个子树尺寸的根节点深搜索确定.之后该节点然后除去,,还有剩下的 ...

  10. POJ 1655 Balancing Act (树状dp入门)

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

随机推荐

  1. kafka topic制定规则

    kafka topic的制定,我们要考虑的问题有很多,比如生产环境中用几备份.partition数目多少合适.用几台机器支撑数据量,这些方面如何去考量?笔者根据实际的维护经验,写一些思考,希望大家指正 ...

  2. ios下引用MUI后input不能输入,Android端正常

    原因是mui框架的有个css样式 *{ -webkit-user-select: none; } 其作用是禁掉用户可以选中页面中的内容. 添加以下style样式即可 input{ -webkit-us ...

  3. 设计四个线程,其中两个线程每次对j增加1,另外两个线程对j每次减1,写出程序

    /* * 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1.写出程序. */ public class ThreadTest { private int j; public sta ...

  4. npm安装包时 --save 和 --save-dev 的区别

    以npm 安装 vue为例 1.npm install vue: 会把vue包安装到node_modules目录中: 不会修改package.json文件: 之后运行npm install命令时,不会 ...

  5. Mysql是否开启binlog日志&开启方法

    运行sql   show variables like 'log_bin'; 如果Value 为 OFF 则为开启日志文件 如何开启mysql日志? 找到my,cnf 中 [mysqld]  添加如下 ...

  6. sublime text 3安装 package control 插件的方法

    自动安装的方法 - 快捷键ctrl+` 或者View->Show Console,输入如下代码 import  urllib.request,os;pf='Package Control.sub ...

  7. C# set 跟 get

    可以在类里面 private string name; public string Name { get { return name; } set { name = value; } }

  8. Java基础—IO小结(二)缓冲流与其它流的使用

    一.缓冲流的使用 每个字节流都有对应的缓冲流: BufferedInputStream / BufferedOutputStream 构造器: 方法摘要与对应节点流类似 使用缓冲流实现文件复制:实际中 ...

  9. The Git Parable:Git传说(转)

    The Git Parable:Git传说 -------- 毛球子好为人师 原文地址:http://tom.preston-werner.com/2009/05/19/the-git-parable ...

  10. Splay初学习

    例题传送门 听YZ哥哥说Splay是一种很神奇的数据结构,所以学习了一下它的最基本操作.O(1)的Spaly. 伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(logn)内完成 ...