题目链接

虽然题目不难,但是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)的更多相关文章

  1. POJ 3107.Godfather 树形dp

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7536   Accepted: 2659 Descrip ...

  2. poj 3107 Godfather 求树的重心【树形dp】

    poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...

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

    关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...

  4. # [Poj 3107] Godfather 链式前向星+树的重心

    [Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...

  5. [POJ 1155] TELE (树形dp)

    题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...

  6. Apple Tree POJ - 2486 (树形dp)

    题目链接: D - 树形dp  POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...

  7. Anniversary party POJ - 2342 (树形DP)

    题目链接:  POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...

  8. POJ 3342 (树形DP)

    题意 :给出一些上下级关系,要求i和i的直接上级不能同时出现,现在选出一些人构成一个集合,问你这个集合里面的最大人数是都少,同时给出这个最大的人数的集合是否唯一. 思路:树形DP,dp[i][0],表 ...

  9. POJ 2342 (树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 ...

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

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

随机推荐

  1. Swift-2-基本操作符

    // Playground - noun: a place where people can play import UIKit // 基本运算符 // 运算符有3种: 单目运算符(如 -a),二目运 ...

  2. Cross Validation done wrong

    Cross Validation done wrong Cross validation is an essential tool in statistical learning 1 to estim ...

  3. 《JavaScript DOM编程艺术》

    第2章JS语法关联数组在为新元素给出下标时,不必局限于整数数字.数组下标可以是字符串逻辑与&&只有两个操作数都是true时结果才为true逻辑或||只有两个操作数都是false时结果才 ...

  4. iOS7光标问题

    iOS7光标问题 有网友遇到textView在ios7上出现编辑进入最后一行时光标消失,看不到最后一行,变成盲打,stackOverFlow网站上有大神指出,是ios7本身bug,加上下面一段代码即可 ...

  5. 异步JS:$.Deferred的使用

    异步JS:$.Deferred的使用 原文链接:http://www.html5rocks.com/en/tutorials/async/deferred/ 当我们构建一个平稳的,响应式的HTML5应 ...

  6. spring事务认识

    Spring配置异常回滚采用的是rollback-for=“BusinessException”.来源于java的检查性异常.非检查性异常的区别.使用spring难免要用到spring的事务管理,要用 ...

  7. sql注入分类

    Sql注入根据数据提取通道的类型,从服务器接收到的响应等可以分为不同的类型. 基于从服务器接收到的响应 ▲基于错误的SQL注入 ▲联合查询的类型 ▲堆查询注射 ▲SQL盲注 •基于布尔SQL盲注 •基 ...

  8. PHP READ PPT FILE

    function parsePPT($filename) { // This approach uses detection of the string "chr(0f).Hex_value ...

  9. 机器学习在 IT 运维管理中的必要性!

    机器学习技术在监控工具中的应用已经成为 IT 运维与 DevOps 团队的一大热点话题.尽管相关的使用案例很多,对 IT 团队而已真正的「杀手级应用」是机器学习如何提高实时事件管理能力,从而帮助较大规 ...

  10. POJ 2105

    #include <iostream> #include <cmath> #include <string> using namespace std; int ma ...