链接:http://poj.org/problem?id=1655

Time Limit: 1000MS Memory Limit: 65536K

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

题意:

给出一棵 $N$ 个节点(编号为 $1 \sim N$)的树。

对于树上某一个节点 $x$,如果我们把它从树中删除,那么原来的一棵树就可能会变成若干棵树,或者说,一个森林;

设 ${\rm{maxpart}}(x)$ 为该森林中节点最多的那一棵树的大小。那么,使得 ${\rm{maxpart}}(x)$ 取得最小值的节点就称为树的重心。

本题要求给出树的重心的编号(如果有多个重心,则给出其中编号最小的),以及其对应的 ${\rm{maxpart}}(x)$。

题解:

实际上,一次DFS就可以求得重心和对应的 ${\rm{maxpart}}(x)$。

如图,节点 $4$ 就是这棵树的重心,且 ${\rm{maxpart}}(4) = \max(n-size[4],size[4],size[6])$。

(图片转载自李煜东《算法竞赛进阶指南》

AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=2e4+; int n; struct Edge{
int u,v;
Edge(int _u=,int _v=){u=_u,v=_v;}
};
vector<Edge> E;
vector<int> G[maxn];
void init(int l,int r)
{
E.clear();
for(int i=l;i<=r;i++) G[i].clear();
}
void addedge(int u,int v)
{
E.push_back(Edge(u,v));
G[u].push_back(E.size()-);
} int siz[maxn],vis[maxn];
pair<int,int> center;
void dfs(int now)
{
vis[now]=; siz[now]=;
int maxpart=;
for(int i=;i<G[now].size();i++)
{
Edge &e=E[G[now][i]]; int nxt=e.v;
if(vis[nxt]) continue;
dfs(nxt);
siz[now]+=siz[nxt];
maxpart=max(maxpart,siz[nxt]);
}
maxpart=max(maxpart,n-siz[now]);
if(maxpart<center.first || (maxpart==center.first && now<center.second))
{
center.first=maxpart;
center.second=now;
}
} int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d",&n);
init(,n);
for(int i=,u,v;i<n;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
} center=make_pair(INF,);
memset(vis,,sizeof(vis));
dfs();
printf("%d %d\n",center.second,center.first);
}
}

POJ 1655 - Balancing Act - [DFS][树的重心]的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. POJ 1655 Balancing Act 焦点树

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

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

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

随机推荐

  1. Linux-文件描述符的本质及与文件指针的区别

    文章参考:文件描述符的本质.文件描述符和文件指针的区别.文件描述符fd和文件指针flip的理解 推荐:task_struct 和文件系统的关系 系统中文件相关表 右侧的表称为i节点表,在整个系统中只有 ...

  2. <转>vmp3.0.9全保护拆分解析

    以下为了避免插件干扰,故采用x64dbg原版进行分析. 首先我通过检测到调试器的弹窗进行栈回溯,定位到该关键点:CALL eax   由于才接触Vmp,所以是把各个保护拆分开来进行的分析,会比较简单一 ...

  3. [转]/etc/passwd文件解析

    /etc/passwd文件内容如下 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daem ...

  4. Mysql线程池系列一:什么是线程池和连接池( thread_pool 和 connection_pool)

       thread_pool 和 connection_pool 当客户端请求的数据量比较大的时候,使用线程池可以节约大量的系统资源,使得更多的CPU时间和内存可以高效地利用起来.而数据库连接池的使用 ...

  5. C#中Post请求的两种方式发送参数链和Body的

    POST请求 有两种方式 一种是组装key=value这种参数对的方式 一种是直接把一个字符串发送过去 作为body的方式 我们在postman中可以看到 sfdsafd sdfsdfds publi ...

  6. 查看修改添加环境变量的工具——Rapid Environment Editor

    工欲善其事,必先利其器! 特别是公司或者有其他限制的时候,更需要一个比较简单.实用.强大的工具了! 原来的公司都是小公司,给电脑安装系统.软件等都是自己直接上手,现在在一个大点的公司了,电脑运维有单独 ...

  7. nginx跨域

    在 conf文件server块里面加上: add_header 'Access-Control-Allow-Origin' "$http_origin"; add_header ' ...

  8. GuavaCache学习笔记三:底层源码阅读

    申明:转载自 https://www.cnblogs.com/dennyzhangdd/p/8981982.html 感谢原博主的分享,看到这个写的真好,直接转载来,学习了. 另外也推荐另外一篇Gua ...

  9. slf4j-api、slf4j-log4j12、log4j之间关系

    1. slf4j-api slf4j:Simple Logging Facade for Java,为java提供的简单日志Facade.Facade门面,更底层一点说就是接口.它允许用户以自己的喜好 ...

  10. KVM上如何让虚拟机支持虚拟化(kvm虚拟化的嵌套)

    http://blog.csdn.net/swimming_in_it_/article/details/53320141http://blog.csdn.net/wickedglory/articl ...