poj1655 Balancing Act 找树的重心
id=1655
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9072 | Accepted: 3765 |
Description
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
that are the endpoints of an edge in the tree. No edge will be listed twice, and all edges will be listed.
Output
Sample Input
1
7
2 6
1 2
1 4
4 5
3 7
3 1
Sample Output
1 2
Source
求树的重心。!
树的重心性质是以该点为根的有根树最大子树的结点数最少,也就是让这树更加"平衡",easy知道这样全部的子树的大小都不会超过整个树大小的一半,所以在树分治时防止树退化成链非常实用。
。
求树的重心做法是dfs简单的树dp即可。设son[i]表示以i为根的子树的结点数(包含i,叶子结点就是1),那么son[i]就+=sum(son[j])...然后求以i为根的最大结点数就是max(son[j],n-son[i]),n-son[i]是i的"上方结点"的个数。
关于树的重心一些性质:http://fanhq666.blog.163.com/blog/static/81943426201172472943638/
代码:
/**
* @author neko01
*/
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int INF=0x3f3f3f3f;
const LL inf=(((LL)1)<<61)+5;
const int N=20005;
struct node{
int to,next;
}e[N*2];
int head[N];
int son[N]; //son[i]表示以i为根的子树节点个数包含i
int dp[N]; //dp[i]表示以i为根时的最大子树节点数
int tot;
int n;
void init()
{
tot=0;
clr1(head);
}
void add(int u,int v)
{
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
void dfs(int u,int pre)
{
son[u]=1;
dp[u]=0;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(v!=pre)
{
dfs(v,u);
son[u]+=son[v];
dp[u]=max(dp[u],son[v]);
}
}
dp[u]=max(dp[u],n-son[u]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init();
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(1,0);
int ans1=0,ans2=n+1;
for(int i=1;i<=n;i++)
if(dp[i]<ans2)
{
ans2=dp[i];
ans1=i;
}
printf("%d %d\n",ans1,ans2);
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
poj1655 Balancing Act 找树的重心的更多相关文章
- POJ1655 Balancing Act(树的重心)
树的重心即树上某结点,删除该结点后形成的森林中包含结点最多的树的结点数最少. 一个DFS就OK了.. #include<cstdio> #include<cstring> #i ...
- POJ-1655 Balancing Act(树的重心)
Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...
- POJ1655 Balancing Act (树的重心)
求树的重心的模板题,size[u]维护以u为根的子树大小,f[u]表示去掉u后的最大子树. 1 #include<cstdio> 2 #include<iostream> 3 ...
- poj1655 Balancing Act求树的重心
Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any nod ...
- POJ 1655 Balancing Act【树的重心】
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14251 Accepted: 6027 De ...
- Balancing Act(树的重心)
传送门 Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14070 Accepted: 593 ...
- poj 1655 Balancing Act 求树的重心【树形dp】
poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...
- POJ 1655 Balancing Act ( 树的重心板子题,链式前向星建图)
题意: 给你一个由n个节点n-1条边构成的一棵树,你需要输出树的重心是那个节点,以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...
- POJ 1655 - Balancing Act - [DFS][树的重心]
链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tre ...
随机推荐
- 了解sota字符界面(章节4.1)
4 SOTA操作 4.1 SOTA字符界面 sotaCC是字符界面管理sota系统程序 . 在/.../sota/bin/目录下,启动sotaCC.在终端的该目录下输入指令“./sotaCC”,启动s ...
- 解决删除Volume报错的问题
很久没有遇到过删除Volume出错使得Volume处于Error_Deleting状态的情况了,昨天删除一个Volume时又出现了这个问题,这里顺便把解决方法记录一下. 注意我这里针对的是后端采用is ...
- HDU1584:蜘蛛牌(DFS)
Problem Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么 ...
- Opencv实现图像的灰度处理,二值化,阀值选择
前几天接触了图像的处理,发现用OPencv处理确实比較方便.毕竟是非常多东西都封装好的.可是要研究里面的东西,还是比較麻烦的,首先,你得知道图片处理的一些知识,比方腐蚀,膨胀,仿射,透射等,还有非常多 ...
- Spring中的FactoryBean
从SessionFactory说起: 在使用SSH集成开发的时候,我们有时候会在applicationContext.xml中配置Hibernate的信息,以下是配置SessionFactory的一段 ...
- ContentProvider简要
1.什么是ContentProvider 数据库在Android其中是私有的,当然这些数据包含文件数据和数据库数据以及一些其它类型的数据. 不能将数据库设为WORLD_READABLE,每一个数据 ...
- 【OpenMesh】Some basic operations: Flipping and collapsing edges
这一节中你将学到一些OpenMesh中早已提供的基础操作. 内容包括三角形网格边的翻转以及通过连接邻接的顶点边缘折叠. 三角形网格的翻转(Flipping edges) 考虑到两个邻接面的三角形网格中 ...
- javascript创建类的6种方式
javascript创建类的7种方式 一 使用字面量创建 1.1 示例 var obj={}; 1.2 使用场景 比较适用于临时构建一个对象,且不关注该对象的类型,只用于临时封装一次数据,且不适合代码 ...
- 移动开发中的Scheme跳转说明——Allowing OtherApps to Start Your Activity
Allowing OtherApps to Start Your Activity 为了开发更多人使用的App,我们总希望我们的App能够提供一种接口被其他App调用.如我们常见的 大众点评 与 ...
- hadoop源码下载地址
http://svn.apache.org/repos/asf/hadoop/common/branches/