Godfather
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6812   Accepted: 2390

Description

Last years Chicago was full of gangster fights and strange murders. The chief of the police got really tired of all these crimes, and decided to arrest the mafia leaders.

Unfortunately, the structure of Chicago mafia is rather complicated. There are n persons known to be related to mafia. The police have traced their activity for some time, and know that some of them are communicating with each other. Based on the data collected, the chief of the police suggests that the mafia hierarchy can be represented as a tree. The head of the mafia, Godfather, is the root of the tree, and if some person is represented by a node in the tree, its direct subordinates are represented by the children of that node. For the purpose of conspiracy the gangsters only communicate with their direct subordinates and their direct master.

Unfortunately, though the police know gangsters’ communications, they do not know who is a master in any pair of communicating persons. Thus they only have an undirected tree of communications, and do not know who Godfather is.

Based on the idea that Godfather wants to have the most possible control over mafia, the chief of the police has made a suggestion that Godfather is such a person that after deleting it from the communications tree the size of the largest remaining connected component is as small as possible. Help the police to find all potential Godfathers and they will arrest them.

Input

The first line of the input file contains n — the number of persons suspected to belong to mafia (2 ≤ n ≤ 50 000). Let them be numbered from 1 to n.

The following n − 1 lines contain two integer numbers each. The pair aibi means that the gangster ai has communicated with the gangster bi. It is guaranteed that the gangsters’ communications form a tree.

Output

Print the numbers of all persons that are suspected to be Godfather. The numbers must be printed in the increasing order, separated by spaces.

Sample Input

6
1 2
2 3
2 5
3 4
3 6

Sample Output

2 3

Source

Northeastern Europe 2005, Northern Subregion
题意:
n个点,n-1条无向边,问去掉哪些点能够使得剩下所有的子树中节点数最多的子树的节点数最少,从小到大输出他们
代码:
//两遍dfs,第一次以1为根节点从下到上统计每个节点作为根的子树中共有几个节点,第二遍枚举
//如果去掉某个点那么他的值是他的子节点构成的若干子树和1节点减去该节点形成的树中
//节点数多的那个值,最后找到值最小的节点就行了。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
int n,val[maxn],cnt[maxn],head[maxn],tol,ans[maxn];
struct Edge{
int to,w,next;
}edge[maxn*];
void Add(int x,int y){
edge[tol].to=y;
edge[tol].next=head[x];
head[x]=tol++;
}
void dfs1(int x,int fa){
val[x]=;
for(int i=head[x];i!=-;i=edge[i].next){
int y=edge[i].to;
if(y==fa) continue;
dfs1(y,x);
val[x]+=val[y];
}
}
void dfs2(int x,int fa){
int tmp=;
for(int i=head[x];i!=-;i=edge[i].next){
int y=edge[i].to;
if(y==fa) continue;
dfs2(y,x);
tmp=max(tmp,val[y]);
}
cnt[x]=max(tmp,val[]-val[x]);
}
int main()
{
while(scanf("%d",&n)==){
memset(head,-,sizeof(head));
tol=;
for(int i=;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
Add(x,y);Add(y,x);
}
dfs1(,);
dfs2(,);
int minx=cnt[];
for(int i=;i<=n;i++)
minx=min(minx,cnt[i]);
int nu=;
for(int i=;i<=n;i++)if(cnt[i]==minx){
ans[++nu]=i;
}
for(int i=;i<nu;i++) printf("%d ",ans[i]);
printf("%d\n",ans[nu]);
}
return ;
}

POJ 3107 树形dp的更多相关文章

  1. Fire (poj 2152 树形dp)

    Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...

  2. poj 1463(树形dp)

    题目链接:http://poj.org/problem?id=1463 思路:简单树形dp,如果不选父亲节点,则他的所有的儿子节点都必须选,如果选择了父亲节点,则儿子节点可选,可不选,取较小者. #i ...

  3. poj 2486( 树形dp)

    题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...

  4. poj 3140(树形dp)

    题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...

  5. Strategic game(POJ 1463 树形DP)

    Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 7490   Accepted: 3483 De ...

  6. POJ 2342 树形DP入门题

    有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...

  7. poj 3345 树形DP 附属关系+输入输出(好题)

    题目连接:http://acm.hust.edu.cn/vjudge/problem/17665 参考资料:http://blog.csdn.net/woshi250hua/article/detai ...

  8. POJ 1155 树形DP

    题意:电视台发送信号给很多用户,每个用户有愿意出的钱,电视台经过的路线都有一定费用,求电视台不损失的情况下最多给多少用户发送信号. 转自:http://www.cnblogs.com/andre050 ...

  9. POJ 3342 树形DP+Hash

    这是很久很久以前做的一道题,可惜当时WA了一页以后放弃了. 今天我又重新捡了起来.(哈哈1A了) 题意: 没有上司的舞会+判重 思路: hash一下+树形DP 题目中给的人名hash到数字,再进行运算 ...

随机推荐

  1. commons-lang源码解析之StringUtils

    apache的commons工具包是平时使用最多的工具包之一,对其实现方式需要具体了解.commons-lang version 3.1 empty和blank的区别 StringUtils中判断St ...

  2. springMVC对jsp页面的数据进行校验

    一. 使用注解校验 a) 引入校验依赖包 <dependency> <groupId>javax.validation</groupId> <artifact ...

  3. Image控件显示以byte[]字节数组形式存在的图片

    工作中遇到了这样的一个问题.起初觉得很简单,获得了图片的byte[]后,可以将其转换成内存中的图片对象(如System.Drawing.Image),而后赋给页面的Image控件.尝试后才发现这样根本 ...

  4. 【转】Expressions versus statements in JavaScript

    原文地址:http://www.2ality.com/2012/09/expressions-vs-statements.html Update 2012-09-21: New in Sect. 4: ...

  5. Python的实现分类

    目前流行的Python实现包括CPython,Jython,IronPython,Stackless,PyPy,Cython,Shed Skin. CPython Cpython是Python的标准实 ...

  6. Java版office文档在线预览

    java将office文档pdf文档转换成swf文件在线预览 第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux ...

  7. CSS3制作各种形状图像(转)

    CSS3制作各种形状图像 浏览:1417 | 更新:2015-05-24 14:43 | 标签:css 1 2 3 4 5 6 7 分步阅读 圆形-椭圆形-三角形-倒三角形=左三角形-右三角形-菱形- ...

  8. 软工1816 · Alpha冲刺(3/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 完成了对laravel框架的一整套机制的了解,对后端的处理流程有全面的认识对整 ...

  9. Java核心技术点之接口

    1. 为什么使用接口 Java中的接口是一组对需求的描述.接口通过声明接口方法来对外宣布:“要想具有XX功能,就得按我说的做(即实现接口方法).” 而接口的实现类通过实现相应接口的方法来宣布:“我已经 ...

  10. C++并行编程2

    启动线程 查看线程构造函数,接受一个返回值为void的函数.如下: void do_some_work(); std::thread my_thread(do_some_work); 也可以接受一个重 ...