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. win32绘制自定义类窗口导致绘制11个窗口的解决办法

    上网查了一圈也没有找到解决问题的办法,一旦创建了一个窗口,并且在过程函数中绘制窗口,尤其是一些非子窗口的自定义类窗口,都会生成11个窗口(算上主窗口就是12个),但是使用系统通用控件就不会有这种情况的 ...

  2. truffle运行特殊 无法找到module的处理方法

    https://blog.csdn.net/SnWJy/article/details/80549227 错误描述: ​ truffle项目根目录执行truffle compile时,报错'modul ...

  3. java常见的异常类型

    Exception分为两类:非运行是异常和运行时异常. java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常.A:NullPointerExcepti ...

  4. [C++] OOP - Access Control and Class Scope

    Access Control And Inheritance Protected Member Like private, protected members are unaccessible to ...

  5. 头文件#ifndef #define #endif使用

    想必很多人都看过“头文件中的 #ifndef #define #endif 防止该头文件被重复引用”.但是是否能理解“被重复引用”是什么意思?是不能在不同的两个文件中使用include来包含这个头文件 ...

  6. StrBlobPtr类——weak_ptr访问vector元素

    #include <iostream> #include <memory> #include <string> #include <initializer_l ...

  7. iOS开发应用程序生命周期

    各个程序运行状态时代理的回调: - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSD ...

  8. Jenkins系列-Jenkins升级、迁移和备份

    升级Jenkins Jenkins的开发迭代非常快,每周发布一个开发版本,长期支持版每半年更新一次(ps:大版本更新).如此频繁的更新,怎么升级呢? war:下载新版的war文件,替换旧版本war文件 ...

  9. java 基础--switch--003

    1,break可以省略吗? default中的可以省略,其他的如果省略会执行下一个case,从下一个case的 break中中断.(case穿透) 2,default一定要在最后吗? 不是,可以在任意 ...

  10. WPF 资源应用

    对资源的应用,有好多方法,以下是一些应用,可以参考 1.静态资源: 2.动态资源: 3.项目面板中的资源: 4.图片.声音等资源