链接: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. Android UI系列-----CheckBox和RadioButton(1)

    主要记录一下CheckBox多选框和RadioGroup.RadioButton单选框的设置以及注册监听器 1.CheckBox 布局文件: <LinearLayout xmlns:androi ...

  2. Ubunt 使用Virtualbox虚拟机NAT无法上网解决办法

    我的Ubuntu安装了一个Centos虚拟机,为了SSH和上外网的方便,使用了NAT+host Only方式,实现内网+外网,但是安装好的Centos不能连接外网,很是无语,只能Google了-- 解 ...

  3. Socket网络编程--简单Web服务器(5)

    这一小节我们将实现服务器对get和post的请求进行对cgi程序的调用.对于web服务器以前的章节已经实现了对get和post请求的调用接口,接下来给出对应接口的实现. int WebServer:: ...

  4. 【GMT43智能液晶模块】例程三:CAN通信实验

    实验原理: STM32F429自带有CAN通信接口,本例程通过CAN1与芯片SN65HVD230相连 实现CAN通信,通过回环测试以验证CAN通信功能. 实验现象: 源代码下载链接: 链接:http: ...

  5. 怎么关闭win10和win8快速启动

    电源选项-- 选择电源按钮的功能--- 更改当前不可用的设置-- 快速启动勾去掉

  6. 分布式系统CAP理论与CA选择

    总结: CAP指的是数据一致性.服务可用性.分区容错性:(这里的一致性指的是强一致性,又叫原子性或线性一致性:可用性指的是所有读写操作都要能终止,没有时延上的要求) 分布式系统中P是必选项:在P必选的 ...

  7. 关于ECMP 等价路由

    1.ECMP简介 Equal-CostMultipathRouting,等价多路径.即存在多条到达同一个目的地址的相同开销的路径.当设备支持等价路由时,发往该目的 IP 或者目的网段的三层转发流量就可 ...

  8. (实用)pip源

    Pypi官方源网站的连接速度实在慢点出奇,可以更换为豆瓣的源 vim ~/.pip/pip.conf 添加如下内容即可: [global]index-url=http://pypi.doubam.co ...

  9. Spark学习笔记——数据读取和保存

    spark所支持的文件格式 1.文本文件 在 Spark 中读写文本文件很容易. 当我们将一个文本文件读取为 RDD 时,输入的每一行 都会成为 RDD 的 一个元素. 也可以将多个完整的文本文件一次 ...

  10. 1. ansible-playbook 变量定义与引用

    简单的playbook playbook 是ansible的核心组件,使用的是YAML语法. 下面请看简单的playbook代码 [root@LeoDevops playb]# cat nginx.y ...