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组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...
 
随机推荐
- 事务处理操作(COMMIT,ROLLBACK)。复制表。更新操作UPDATE实际工作中一般都会有WHERE子句,否则更新全表会影响系统性能引发死机。
			
更新操作时两个会话(session)同时操作同一条记录时如果没有事务处理操作(COMMIT,ROLLBACK)则会导致死锁. 复制表:此方法Oracle特有
 - Python2/3的中、英文字符编码与解码输出: UnicodeDecodeError: 'ascii' codec can't decode/encode
			
摘要:Python中文虐我千百遍,我待Python如初恋.本文主要介绍在Python2/3交互模式下,通过对中文.英文的处理输出,理解Python的字符编码与解码问题(以点破面). 前言:字符串的编码 ...
 - JavaWeb之数据源连接池(2)---C3P0
			
我们接着<JavaWeb之数据源连接池(1)---DBCP>继续介绍数据源连接池. 首先,在Web项目的WebContent--->WEB-INF--->lib文件夹中添加C3 ...
 - 地理信息系统公开课计划 前言I
			
对,就是地理信息系统(GIS),不是遥感RS,也不是编程,纯粹的地理信息系统. 地理信息系统=数学+物理+计算机+地理的烧脑组合. 但凡能知道.了解地理信息系统的人,基本上都不会是非知识分子,我就不矫 ...
 - RabbitMQ之消费者Demo(队列参数详细说明)
			
package com.jiefupay; import java.io.IOException; import java.util.HashMap; import java.util.Map; 8 ...
 - ES6 Proxy和Reflect (上)
			
Proxy概述 Proxy用于修改某些操作的默认行为,等同于在语言层面做出修改,所以属于一种"元编程"(meta programming),即对编程语言进行编程. Proxy可以理 ...
 - Linux(CentOS6.5)下编译安装Nginx1.10.1
			
首先在特权账号(root)下安装编译时依赖项: yum install gcc gcc-c++ perl -y 首先以非特权账号(本文以账号comex为例)登陆OS: 进入data目录下载相关安装 ...
 - Spring之DAO二
			
上一篇算是把JdbcDaoSupport的使用演示了一下,这篇主要是演示MappingSqlQuery.SqlUpdate.SqlFunction的使用以及Spring的事务管理. 一.Mapping ...
 - Web程序员们,你准备好迎接HTML5了吗?
			
HTML5作为下一代的web开发标准,其特性已经慢慢地出现在主流的浏览器中,这种新的HTML将会让浏览器不必再依赖Flash.QuickTime.Silverlight等插件,也简化了原来需要大量JS ...
 - SEO的基本概念 和 提交SITEMAP到搜索引擎
			
SEO的基本概念 SEO的基本概念 :我们接触的SEO主要就是做搜索引擎的优化,让搜索引擎更好地收录 我们的网站,不管是首页还是内页,都能有很好的收录和排名.那么我们经常用到的SEO的语句是什么呢?小 ...