树的重心的定义:

树的重心也叫树的质心。找到一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡。
通常利用树形DP找重心:
题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的
清空的时候记得要清干净.....
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
const int maxx=<<;
int t,n;
int f[maxn]={};
int vis[maxn]={};
struct nod{
int y;
int next;
}e[*maxn]={};
int head[maxn]={},tot=;
int ans,size=;
void init(int x,int y){
e[++tot].y=y;
e[tot].next=head[x];
head[x]=tot;
}
void dfs(int x){
int y;
vis[x]=;
f[x]=;
int tmp=;
for(int i=head[x];i;i=e[i].next){
y=e[i].y;
if(!vis[y]){
dfs(y);
f[x]+=f[y]+;
tmp=max(tmp,f[y]+);
}
}tmp=max(tmp,n-f[x]-);
if(tmp<size||(tmp==size&&x<ans)){
size=tmp;
ans=x;
}
}
void yu(){
size=maxx;
ans=maxx;
tot=;
memset(f,,sizeof(f));
memset(vis,,sizeof(vis));
memset(head,,sizeof(head));
memset(e,,sizeof(e));
}
int main(){
scanf("%d",&t);
while(t-->){
yu();
scanf("%d",&n);
int x,y;
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
init(x,y);
init(y,x);
}
dfs();
printf("%d %d\n",ans,size);
}
return ;
}
题意:给定一棵树,求树的所有重心,按照编号从小到大的顺序输出.
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
const int maxx=<<;
int t,n;
int f[maxn]={};
int vis[maxn]={};
struct nod{
int y;
int next;
}e[*maxn]={};
int head[maxn]={},tot=;
int ans[maxn]={},size=,sum=;
void init(int x,int y){
e[++tot].y=y;
e[tot].next=head[x];
head[x]=tot;
}
void dfs(int x){
int y;
vis[x]=;
int tmp=;
for(int i=head[x];i;i=e[i].next){
y=e[i].y;
if(!vis[y]){
dfs(y);
f[x]+=f[y]+;
tmp=max(f[y]+,tmp);
}
}
tmp=max(tmp,n-f[x]-);
if(tmp<size){
size=tmp;
sum=;
ans[++sum]=x;
}
else if(tmp==size){
ans[++sum]=x;
}
}
int main(){
size=maxx;
scanf("%d",&n);
int x,y;
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
init(x,y);
init(y,x);
}
dfs();
sort(ans+,ans++sum);
for(int i=;i<sum;i++){
printf("%d ",ans[i]);
}
printf("%d\n",ans[sum]);
return ;
}

POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)的更多相关文章

  1. POJ 1655 Balancing Act(求树的重心--树形DP)

    题意:求树的重心的编号以及重心删除后得到的最大子树的节点个数size,假设size同样就选取编号最小的. 思路:随便选一个点把无根图转化成有根图.dfs一遍就可以dp出答案 //1348K 125MS ...

  2. POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

  3. poj 3107 Godfather(树的重心)

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7885   Accepted: 2786 Descrip ...

  4. POJ 1655 - Balancing Act - [DFS][树的重心]

    链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tre ...

  5. poj 1655 Balancing Act 求树的重心【树形dp】

    poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...

  6. POJ 1655 Balancing Act【树的重心模板题】

    传送门:http://poj.org/problem?id=1655 题意:有T组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...

  7. POJ 1655 Balancing Act (树的重心,常规)

    题意:求树的重心,若有多个重心,则输出编号较小者,及其子树中节点最多的数量. 思路: 树的重心:指的是一个点v,在删除点v后,其子树的节点数分别为:u1,u2....,设max(u)为其中的最大值,点 ...

  8. POJ - 1655 (点分治-树的重心)

    题目:https://vjudge.net/contest/307753#problem/D 题意:给你一棵树,让你求出一个点,让他的最大子树的节点数尽量小 思路:最大子树节点数尽量小,一看就是树的重 ...

  9. POJ 1655 Balancing Act ( 树的重心板子题,链式前向星建图)

    题意: 给你一个由n个节点n-1条边构成的一棵树,你需要输出树的重心是那个节点,以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...

随机推荐

  1. 数组C - 玛雅日历

    During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calend ...

  2. 【洛谷 P4219】 [BJOI2014]大融合(LCT)

    题目链接 维护子树信息向来不是\(LCT\)所擅长的,所以我没搞懂qwq 权当背背模板吧.Flash巨佬的blog里面写了虽然我没看懂. #include <cstdio> #define ...

  3. 【洛谷 P1772】 [ZJOI2006]物流运输(Spfa,dp)

    题目链接 \(g[i][j]\)表示不走在\(i\text{~}j\)时间段中会关闭的港口(哪怕只关\(1\)天)从\(1\)到\(m\)的最短路. \(f[i]\)表示前\(i\)天的最小花费.于是 ...

  4. nth-child,nth-last-child,only-child,nth-of-type,nth-last-of-type,only-of-type,first-of-type,last-of-type,first-child,last-child伪类区别和用法

    我将这坨伪类分成三组,第一组:nth-child,nth-last-child,only-child第二组:nth-of-type,nth-last-of-type,第三组:first-of-tpye ...

  5. ajax技术整理总结(1)

    1.创建ajax对象 var xhr=new XMLHttpRequest(); 4.监听状态信息 xhr.onreadystatechange=function(){ //4接收完毕 ){ docu ...

  6. javascript经典小游戏代码集合

    http://www.jb51.net/Special/349.htm

  7. JS几个常用的工具函数

    一个项目中JS也不可避免会出现重用,所以可以像Java一样抽成工具类,下面总结了几个常用的函数: 1.日期处理函数 将日期返回按指定格式处理过的字符串: function Format(now,mas ...

  8. 转: oracle中schema指的是什么?

    看来有的人还是对schema的真正含义不太理解,现在我再次整理了一下,希望对大家有所帮助. 我们先来看一下他们的定义:A schema is a collection of database obje ...

  9. 在ubuntu中安装puppeteer

    https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md 早些时候puppeteer刚出来,在vps上 ...

  10. Centos更新配置文件命令

    source 命令是 bash shell 的内置命令,从 C Shell 而来.source 命令的另一种写法是点符号,用法和 source 相同,从Bourne Shell而来.source 命令 ...