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 ...
随机推荐
- Opencv笔记(1) 命名规则数据结构(CvMat,...)
网上搜索了很多,检查中发现的信息劣势,检查源代码 同Cv为类的开始.包含详细的数据不(仅存储指针) CvMat typedef struct CvMat { int type; int step; / ...
- Delphi与Vista提供的UAC控制(1-代表资源编号,24-资源类型为RTMAINIFEST,最后用brcc32编译成资源文件)
Vista提供的UAC机制,是Vista的新增功能之一.它的主要目的是防止对于操作系统本身的恶意修 改.如果想对于Vista的 系统设置进行改动,必须通过UAC的验 证才能够进行.通过这样的手段,大大 ...
- Javascript selection的兼容性写法介绍
本文为大家讲解下Javascript selection的兼容性写法,感兴趣的朋友可以参考下 function getSelectedText() { //this function code is ...
- Shell 传递参数
Shell 传递参数 向脚本传递参数,格式为:$n. 向脚本传递三个参数,并分别输出: echo "Shell 传递参数实例!"; echo "第一个参数为:$1&quo ...
- discuz!代码内置颜色大全(收藏)
加闪烁字:[light]文字[/light] 加文字特效:[shadow=255,red,2]文字[/shadow]: 在标签的中间插入文字可以实现文字阴影特效,shadow内属性依次为宽度.颜色和边 ...
- Java深入解析读书笔记(一)
1. goto,const为java的两个保留关键字,无任何应用语法.因此从不使用. goto 使用循环标签:if,break out,here实现goto的功能. 2. 标识符:可由字母数字下划 ...
- 为什么EXE文件出现了不该出现的“盾牌”
下载了一个小程序,它的功能并不需要管理员权限.但是在Win7下面它的图标上出现了一个“小盾牌”,这意味着运行它需要提升权限……果然,双击时弹出了UAC对话框.用二进制编辑器打开这个EXE,发现它没有内 ...
- Android资源管理框架(Asset Manager)简介和学习计划
Android该应用程序包括两个部分组成的:代码和资源. 资源主要是与UI相关的东西,例如UI布局.和其他字符串和照片.代码和资源可以使独立的应用程序来组织的实际需求的基础上,在执行的时候UI.,就能 ...
- js动态添加Div
利用JavaScript动态添加Div的方式有很多,在这次开发中有用到,就搜集了一下比较常用的. 一.在一个Div前添加Div <html> <body> <div id ...
- Linux几种关机(重启)相关命令
在linux下一些常用的关机/重启命令有shutdown.halt.reboot.及init,它们都可以达到重启系统的目的,但每个命令的内部工作过程是不同的,通过本文的介绍,希望你可以更加灵活的运用各 ...