POJ 3107 Godfather (树形dp)
虽然题目不难,但是1A还是很爽, 只是刚开始理解错题意了,想了好久。 还有据说这个题用vector会超时,看了以后还是用邻接吧。
题意:
给一颗树,保证是一颗树,求去掉一个点以后的联通块里节点的数目的 最大值最小,求这样的点,并按照递增顺序输出。
分析:
d[father] = max(n-sum, d[son]); sum代表这个节点以下的全部节点总数, 去掉一个节点的联通块的最大的节点数 等于 整个树里的节点数减去这个节点下的总数 和 子树的数目的
最大值。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#define LL __int64
const int maxn = +;
const int INF = <<;
using namespace std;
int head[maxn], vis[maxn], t, d[maxn];
int mi, n;
struct node
{
int u, v, ne;
}e[*maxn]; void add(int u, int v)
{
e[t].u = u;
e[t].v = v;
e[t].ne = head[u];
head[u] = t++;
}
int dfs(int son, int fa)
{
int i, tmp, sum = , x; //sum是以son为根节点的子树的全部的节点数
for(i = head[son]; i != -; i = e[i].ne)
{
tmp = e[i].v;
if(tmp == fa) continue; //避免回去。
x = dfs(tmp, son);
sum += x;
d[son] = max(d[son], x);
}
d[son] = max(d[son], n-sum);
if(d[son]<mi)
mi = d[son];
return sum;
}
int main()
{
int i, f;
while(~scanf("%d", &n))
{
memset(e, , sizeof(e));
memset(head, -, sizeof(head));
memset(vis, , sizeof(vis));
memset(d, , sizeof(d));
t = ;
mi = INF; for(i = ; i < n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs(, -); //把给的树看成以1为根节点。 f = ;
for(i = ; i <= n; i++)
{
if(d[i]==mi)
{
if(f)
printf(" %d", i);
else
printf("%d", i);
f = ;
}
}
printf("\n");
}
return ;
}
避免回去的时候也可以用vis[]来标记
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#define LL __int64
const int maxn = +;
const int INF = <<;
using namespace std;
int head[maxn], vis[maxn], t, d[maxn];
int mi, n;
struct node
{
int u, v, ne;
}e[*maxn]; void add(int u, int v)
{
e[t].u = u;
e[t].v = v;
e[t].ne = head[u];
head[u] = t++;
}
int dfs(int son)
{
int i, tmp, sum = , x;
vis[son] = ;
for(i = head[son]; i != -; i = e[i].ne)
{
tmp = e[i].v;
if(vis[tmp]) continue;
x = dfs(tmp);
sum += x;
d[son] = max(d[son], x);
}
d[son] = max(d[son], n-sum);
if(d[son]<mi)
mi = d[son];
return sum;
}
int main()
{
int i, f;
while(~scanf("%d", &n))
{
memset(e, , sizeof(e));
memset(head, -, sizeof(head));
memset(vis, , sizeof(vis));
memset(d, , sizeof(d));
t = ;
mi = INF; for(i = ; i < n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs(); f = ;
for(i = ; i <= n; i++)
{
if(d[i]==mi)
{
if(f)
printf(" %d", i);
else
printf("%d", i);
f = ;
}
}
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 题目大意: 直接给你一棵树,让你求树的 ...
随机推荐
- AD转换后数字量的处理
假设模拟输入电压的最大值为5V,A/D转换器件为8位转换. [该转换器的分辨率为1/2n=0.3906%.] [能分辨输入模拟电压变化的最小值为5*0.3906%=19.5mv.] 则模拟电压与数字输 ...
- linux下解压命令大全(转载)
.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压 ...
- mongodb维护常用命令
一,用户操作:1. #进入数据库adminuse admin2. #增加或修改用户密码db.addUser('name','pwd')3. #查看用户列表db.system.users.find()4 ...
- Ajax风格的一款网页Loading效果
现在比较流行的一款Ajax风格的网页Loading,多见于一些大量使用Ajax技术的网站中,页面加载时会自动显示提示信息,带载入动画效果,网页加载完自动消失,是一款正在具有Loading功能的网页进度 ...
- Linux Rsync
一.Rsync介绍 1.什么是Rsync Rsync 即Remote Rynchronization,是一款开源的.快速的.多功能的.可实现全量或增量的本地或者远程数据镜像同步复制.备份的优秀工具. ...
- Python并发与并行的新手指南
点这里 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global interpreter lock(也被亲切的称为“GIL”)指指点点,说它阻碍了Python的多线程 ...
- C++ 第一次上机作业
今天完成了C++第一次上机作业,感觉比较简单. 题目: 求2个数或3个正整数中的最大数,用带有默认参数的函数实现. 对3个变量按由小到大顺序排序,要求使用变量的引用. 编写一个程序,用同一个函数名对几 ...
- HDU 2059 龟兔赛跑(动态规划)
龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 解析Java中静态变量与实例变量的区别
java类的成员变量有俩种:一种是被static关键字修饰的变量,叫类变量或者静态变量:另一种没有static修饰,为实例变量. 在语法定义上的区别:静态变量前要加static关键字,而实例 ...
- lintcode:买卖股票的最佳时机 II
买卖股票的最佳时机 II 假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再 ...