POJ 1655 - Balancing Act - [DFS][树的重心]
链接: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][树的重心]的更多相关文章
- poj 1655 Balancing Act 求树的重心【树形dp】
poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...
- POJ 1655 Balancing Act【树的重心】
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14251 Accepted: 6027 De ...
- POJ 1655 Balancing Act【树的重心模板题】
传送门:http://poj.org/problem?id=1655 题意:有T组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...
- POJ 1655 Balancing Act(求树的重心--树形DP)
题意:求树的重心的编号以及重心删除后得到的最大子树的节点个数size,假设size同样就选取编号最小的. 思路:随便选一个点把无根图转化成有根图.dfs一遍就可以dp出答案 //1348K 125MS ...
- POJ 1655 Balancing Act (树的重心,常规)
题意:求树的重心,若有多个重心,则输出编号较小者,及其子树中节点最多的数量. 思路: 树的重心:指的是一个点v,在删除点v后,其子树的节点数分别为:u1,u2....,设max(u)为其中的最大值,点 ...
- POJ 1655 Balancing Act ( 树的重心板子题,链式前向星建图)
题意: 给你一个由n个节点n-1条边构成的一棵树,你需要输出树的重心是那个节点,以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...
- POJ 1655 Balancing Act (求树的重心)【树形DP】(经典)
<题目链接> 题目大意:给你一棵树,任意去除某一个点后,树被分成了几个联通块,则该点的平衡值为所有分成的连通块中,点数最大的那个,问你:该树所有点中,平衡值最小的那个点是什么? 解题分析: ...
- POJ 1655 Balancing Act 焦点树
标题效果:鉴于一棵树.除去一个点之后,这棵树将成为一些中国联通的块.之后该点通过寻求取消最低形成块的最大数目. 思维:树DP思维.通过为每个子树尺寸的根节点深搜索确定.之后该节点然后除去,,还有剩下的 ...
- POJ 1655 Balancing Act (树状dp入门)
Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any nod ...
随机推荐
- C++ 重载运算符和重载函数
C++ 重载运算符和重载函数 C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载. 重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是 ...
- php手册总结《类》
手册页面: http://php.net/manual/zh/language.oop5.basic.php >> 类名 类名可以是任何非 PHP 保留字的合法标签.一个合法类名以字母或下 ...
- 建立一个基本的UI
本章让你熟悉Xcode来写应用程序.你会熟悉Xcode项目的结构,并学习如何在基本项目组件中导航.通过整个课程中,您将开始为FoodTracker应用程序制作一个简单的用户界面(UI),并在模拟器查看 ...
- 【iCore4 双核心板_ARM】例程十八:USBD_VCP实验——虚拟串口
实验步骤: 1.将跳线冒跳至USB_OTG,通过Micro USB 线将iCore4 USB-OTG接口与电脑相连. 2.打开设备管理器,可以找到虚拟出来的端口,(特殊情况下如果没有虚拟出端口,我们可 ...
- File 类的 getCanonicalFile( ) 和 getAbsoluteFile( ) 区别
一.打开java.io.File源码,看下两个方法的区别 getAbsoluteFile public File getAbsoluteFile() { String absPath = getAbs ...
- maven 如何引入本地jar包
比如我下载了 一.怎么添加jar到本地仓库呢?步骤:1.cmd命令进入该jar包所在路径2.执行命令:mvn install:install-file -Dfile=lucene-queryparse ...
- qt在GUI显示时,将调试信息输出到控制台的设置
1. 在.pro文件中添加一下设置: CONFIG += console 2. 项目的[构建和运行]中,需要勾选[Run in terminal]:
- Dapper的基本使用,Insert、Update、Select、Delete
简介 Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,并且是半自动的.也就是说实体类都要自己写.它没有复杂的配置文件,一个单文 ...
- 给hmailserver添加DKIM签名
上一篇说了hmailserver如何设置反垃圾邮件功能,现在来说说如何让自己的hmailserver发出去的邮件不要被别人反垃圾了.在hmailserver的反垃圾邮件功能中有提到给垃圾评分标准,其中 ...
- iOS - UIView属性hidden, opaque, alpha, opacity的区别
iOS开发-之UIView属性hidden, opaque, alpha, opacity的区别 一.alpha 液晶显示器是由一个个的像素点组成的,每个像素点都可以显示一个由RGBA颜色空间组成的一 ...