POJ 1655 Balancing Act【树的重心】
Balancing Act
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 14251 | Accepted: 6027 |
Description
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
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
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
Source
题目很好理解,就是去掉树上的一个节点,看看剩下的子树中最大的是多少,然后在这些最大值中求一个最小值,如果有多个点都是最小值,那么找一个序号最小的节点。输出节点号,和最小值。
经过简单分析,DFS深度优先搜索可以解决,只需要求出每个节点下子树的总结点个数即可。
这题其实是求重心的裸题!
举例说明:
设有一棵树20个节点,其中有一个节点为u,u有两个孩子节点,设u以下有10个节点,两个孩子分别有6和4个节点,那么对于u来说,最大是多少,应该是20 - 10,6,4中的最大的也就是10.这样等把所有节点的最大值求出后,再求1-n中的最小值,输出该点以及最小值即可。
算法就是DFS,计算出每个节点下的总数,然后保留本节点下的孩子节点子树中的最大值,然后和自己的祖先节点比较求出最大值,最后枚举最小值。
这题一定要建一个双向图,因为树是无向的。
下面是AC代码:
#include <stdio.h>
#include <string.h>
#define max(a,b) (a)>(b)?(a):(b)
const int maxn=;
struct Edge//前向星存边
{
int to,next;
}edge[maxn<<];//保存双向图
int head[maxn],tot;
inline void init()//初始化操作
{
memset(head,-,sizeof(head));
tot=;
}
inline void addedge(int u,int v)//加边
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
int dp[maxn];//每个节点去掉后其他子树的点数
int num[maxn];//每个节点下的节点总数
int n;
inline void DFS(int u,int pre)
{
dp[u]=;//初始化
num[u]=;//初始化
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(v==pre)
continue;
DFS(v,u);
dp[u]=max(dp[u],num[v]);//求根节点下儿子节点中的最大值
num[u]+=num[v];//求根节点下的所有节点数
}
dp[u]=max(dp[u],n-num[u]);//祖先节点总数的最大值和根节点下儿子节点中的最大值作为本节点的最大值
}
int main()
{
int T;
scanf("%d",&T);
int u,v;
while(T--)
{
scanf("%d",&n);
init();
for(int i=;i<n;i++)//建图
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
DFS(,-);
int ans1=,ans2=dp[];
for(int i=;i<=n;i++)//求最小值
{
if(ans2>dp[i])
{
ans1=i;
ans2=dp[i];
}
}
printf("%d %d\n",ans1,ans2);
}
return ;
}
POJ 1655 Balancing Act【树的重心】的更多相关文章
- POJ 1655 Balancing Act 树的重心
Balancing Act Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. ...
- POJ 1655 - Balancing Act 树型DP
这题和POJ 3107 - Godfather异曲同工...http://blog.csdn.net/kk303/article/details/9387251 Program: #include&l ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
- poj 1655 Balancing Act 求树的重心【树形dp】
poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...
- POJ 1655.Balancing Act 树形dp 树的重心
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14550 Accepted: 6173 De ...
- poj 1655 Balancing Act(找树的重心)
Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或 ...
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
- POJ 1655 - Balancing Act - [DFS][树的重心]
链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tre ...
- POJ 1655 Balancing Act【树的重心模板题】
传送门:http://poj.org/problem?id=1655 题意:有T组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...
随机推荐
- iOS UITabView简写瀑布流
代码demo 一.tabViewCell,通过image的比例高算出cell 的高度 #import "TableViewCell.h" @implementation Table ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- ArcGIS 网络分析[8.5] 资料5 网络分析拓展模块及各种接口说明
说完了网络数据集如何用AO来创建,接下来就说说如何用另一个类库--NetworkAnalyst来做网络分析. 区别于NetworkAnalysis,这个是几何网络分析用的类库. 本篇先做类库和有关接口 ...
- CJOJ 血帆海盗
Description 随着资本的扩大,藏宝海湾贸易亲王在卡利姆多和东部王 国大陆各建立了N/2 个港口.大灾变发生以后,这些港口之间失去了联系,相继脱离了藏宝海湾贸易亲王的管辖,各自为政.利益的驱动 ...
- solr 的 field, copyfield ,dynamic field
Field: Field就是一个字段,定义一个Field很简单: <field name="price" type="sfloat" indexed=&q ...
- Spring之DAO二
上一篇算是把JdbcDaoSupport的使用演示了一下,这篇主要是演示MappingSqlQuery.SqlUpdate.SqlFunction的使用以及Spring的事务管理. 一.Mapping ...
- python编辑器pydev安装
pydev实际上时eclipse的插件 1.安装eclipse(1)eclipse不需要安装,解压文件夹,点击eclipse.exe可使用 2.安装pydev (1) 解压pydev可看到featur ...
- Netty对Protocol Buffer的支持(七)
Netty对Protocol Buffer的支持(七) 一.简介 在上一篇博文中笔者已经介绍了google的Protocol Buffer的使用,那么本文笔者就开始介绍netty对Protocol B ...
- Linux上安装Redis
很多编程的小朋友一提到Linux脑袋就大了,我也一样,我是一个大专的学生,没有学过Linux,感觉自己欠缺很多,也知道了人和人之间的差距,当你真正的走上社会,才知道社会是什么,才知道没有学历找工作有多 ...
- git强制push
Git 如何强制push? $ git push -u origin master –f 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing 欢迎关注,有 ...