POJ 3107-Godfather(树形dp)
题意:
有n个节点的树,删除一个点,得到的最大联通分支最小,求这样点的集合
分析:
dp[i]表示删除i所得最大联通分支,遍历一遍节点即可,该题用vector会超时
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 50010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int dp[N],n,num[N],used[N],ans[N],len,head[N];
struct edge{
int t,next;
}e[N*];
int max(int a,int b){
return a>b?a:b;
}
void add(int a,int b){
e[len].t=b;
e[len].next=head[a];
head[a]=len++;
}
int dfs(int root){
num[root]=;
used[root]=;
for(int i=head[root];i!=-;i=e[i].next){
int son=e[i].t;
if(used[son])continue;
num[root]+=dfs(son);
dp[root]=max(dp[root],num[son]);
}
dp[root]=max(dp[root],n-num[root]);
return num[root];
}
int main()
{
while(~scanf("%d",&n)){
memset(dp,,sizeof(dp));
memset(used,,sizeof(used));
memset(head,-,sizeof(head));
int a,b;
len=;
for(int i=;i<n-;++i){
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
dfs();
int l,minv=INF;
for(int i=;i<=n;++i){
if(dp[i]<minv){
l=;
ans[l++]=i;
minv=dp[i];
}
else if(dp[i]==minv)
ans[l++]=i;
}
for(int i=;i<l;++i){
printf("%d ",ans[i]);
}
printf("\n");
}
return ;
}
POJ 3107-Godfather(树形dp)的更多相关文章
- POJ 3107.Godfather 树形dp
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7536 Accepted: 2659 Descrip ...
- poj 3107 Godfather 求树的重心【树形dp】
poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
- # [Poj 3107] Godfather 链式前向星+树的重心
[Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...
- [POJ 1155] TELE (树形dp)
题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...
- Apple Tree POJ - 2486 (树形dp)
题目链接: D - 树形dp POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...
- Anniversary party POJ - 2342 (树形DP)
题目链接: POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...
- POJ 3342 (树形DP)
题意 :给出一些上下级关系,要求i和i的直接上级不能同时出现,现在选出一些人构成一个集合,问你这个集合里面的最大人数是都少,同时给出这个最大的人数的集合是否唯一. 思路:树形DP,dp[i][0],表 ...
- POJ 2342 (树形DP)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3863 Accepted: 2172 ...
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
随机推荐
- 【BZOJ 1951】 [Sdoi2010]古代猪文
Description “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心……” ——选自猪王国民歌 很久很久以前,在山的那边 ...
- linux常用命令及安装软件命令
1.查看操作系统是33位还是64最简单的方法 getconf LONG_BIT 或者 uname -a 2.常用命令 2.1基本操作 clear 清屏 2.2安装命令 rpm(redhat packa ...
- 拼音操作工具类 - PinyinUtil.java
拼音操作工具类,提供字符串转换成拼音数组.汉字转换成拼音.取汉字的首字母等方法. 源码如下:(点击下载 -PinyinUtil.java.pinyin4j-2.5.0.jar ) import net ...
- c++ 16 this 和 继承 及继承机制中的构造函数 与 析构函数
#include <iostream> #include <string> using namespace std; class Animal { public: Animal ...
- 结构体 typedef关键字
1 结构体 #include <iostream> #include <cstring> using namespace std; void printBook( struct ...
- Howto: Deploy VC2008 apps without installing vcredist_x86.exe
There are several reasons for xcopy deployment of an application (also known as application local). ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- php关于private、public成员变量访问问题
如果类里面定义了__get($name)方法,则不论类的private成员还是public成员,都能够在类的外面通过类似$class->name访问到.如果是public变量,则不会自动调用ge ...
- Discuz模版与插件 安装时提示“对不起,您安装的不是正版应用...”解决方法
关于出现“对不起,您安装的不是正版应用..”的解决办法 有些插件和风格在安装时出现不能安装的现象,出现以下提示: 对不起,您安装的不是正版应用,安装程序无法继续执行 点击这里安 ...
- 【TopCoder】SRM 680 DIV 2
1. BearPair之bigDistance1.1 题目概述在 <= 50的字符串中找位置i,j 满足(1) s[i] != s[j];(2) abs(i-j)尽可能大.若不存在返回-1, 否 ...