POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)
树的重心的定义:
#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)的更多相关文章
- POJ 1655 Balancing Act(求树的重心--树形DP)
题意:求树的重心的编号以及重心删除后得到的最大子树的节点个数size,假设size同样就选取编号最小的. 思路:随便选一个点把无根图转化成有根图.dfs一遍就可以dp出答案 //1348K 125MS ...
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
- poj 3107 Godfather(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7885 Accepted: 2786 Descrip ...
- 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 求树的重心【树形dp】
poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...
- POJ 1655 Balancing Act【树的重心模板题】
传送门:http://poj.org/problem?id=1655 题意:有T组数据,求出每组数据所构成的树的重心,输出这个树的重心的编号,并且输出重心删除后得到的最大子树的节点个数,如果个数相同, ...
- POJ 1655 Balancing Act (树的重心,常规)
题意:求树的重心,若有多个重心,则输出编号较小者,及其子树中节点最多的数量. 思路: 树的重心:指的是一个点v,在删除点v后,其子树的节点数分别为:u1,u2....,设max(u)为其中的最大值,点 ...
- POJ - 1655 (点分治-树的重心)
题目:https://vjudge.net/contest/307753#problem/D 题意:给你一棵树,让你求出一个点,让他的最大子树的节点数尽量小 思路:最大子树节点数尽量小,一看就是树的重 ...
- POJ 1655 Balancing Act ( 树的重心板子题,链式前向星建图)
题意: 给你一个由n个节点n-1条边构成的一棵树,你需要输出树的重心是那个节点,以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...
随机推荐
- 深入理解Spring系列之五:BeanDefinition装载
转载 https://mp.weixin.qq.com/s/1_grvpJYe8mMIAnebMdz9Q 接上篇<深入理解Spring系列之四:BeanDefinition装载前奏曲>,进 ...
- 很多人都没用过的轻量级Oracle数据库数据导出工具SQLLDR2——性能超赞
SQLLDR2 介绍 每周发表一篇数据库或大数据相关的帖子,敬请关注 1. 工具介绍 Sqluldr2(SQL * UnLoader 第二版)是灵活与强大的 Oracle 文本导出程序,已被大众使 用 ...
- 11.Container With Most Water---两指针
题目链接:https://leetcode.com/problems/container-with-most-water/description/ 题目大意:给出一串数组(a1, a2, a3, .. ...
- Python递归 — — 二分查找、斐波那契数列、三级菜单
一.二分查找 二分查找也称之为折半查找,二分查找要求线性表(存储结构)必须采用顺序存储结构,而且表中元素顺序排列. 二分查找: 1.首先,将表中间位置的元素与被查找元素比较,如果两者相等,查找结束,否 ...
- ssh使两台机器建立连接
ssh利用口令建立连接过程: 客户端--> 发送连接请求 --> 远程主机 --> 返回远程主机的公钥 --> 公钥加密客户端私钥+客户端公钥返回远程主机 --> 远程主 ...
- Windows 和Linux 误删除后的恢复
ext文件系统上删除文件,可以恢复:extundelete; windows 恢复删除文件: final data v2.0汉化版 和 easyrecovery
- PlantUML——1.Hello
官网: http://www.plantuml.com/ 第一步: 下载 plantuml.jar文件: 第二步:创建一个demo.txt文件(与plantuml.jar在同一目录),内容如下: @s ...
- Percona XtraDB Cluster(PXC)-高可用架构设计说明
Mycat+PXC高可用集群 一.架构图 架构说明: 1.mysql 集群高可用部分: l 针对业务场景选用Percona XtraDB Cluter(PXC)复制集群.两个片集群 PXC-dataN ...
- POJ 3253 Fence Repair(哈夫曼编码)
题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...
- Error: could not open `C:\Java\jre7\lib\i386\jvm.cfg
打开eclipse时出现Error: could not open `C:\Program Files\Java\jre7\lib\i586\jvm.cfg’) 删除 c:\windows\syste ...