POJ 3107 树形dp

时间:2022-06-16 04:42:37
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 &lpar;poj 2152 树形dp&rpar;

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

  2. poj 1463&lpar;树形dp&rpar;

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

  3. poj 2486&lpar; 树形dp&rpar;

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

  4. poj 3140&lpar;树形dp&rpar;

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

  5. Strategic game&lpar;POJ 1463 树形DP&rpar;

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

  6. POJ 2342 树形DP入门题

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

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

    题目连接: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&plus;Hash

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

随机推荐

  1. C&num;Linq技术中SelectMany&lpar;&period;&period;&period;&rpar;的内部实现推测

    对于声明为:public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable&l ...

  2. Devexpress RaisePropertyChanged

    所有的重载设置字段作为参数传递到指定的值,而属性提高INotifyPropertyChanged.PropertyChanged事件. 如果一个字段已经成功地改变,setProperty方法中返回真. ...

  3. JQuery插件让图片旋转任意角度且代码极其简单

    引入下方的jquery.rotate.js文件,然后通过$("选择器").rotate(角度);可以旋转任意角度, 例如$("#rotate-image").r ...

  4. J2EE 第二阶段项目之部署项目、分工安排

    SVN 先通过使用教程,和能够介绍了解svn. svn使用教程总结   ;   svn功能介绍. 分工安排:我的任务就是项目统计. 1 效益统计 1 教育效益统计表 (教育效益统计表,增,改,查看,查 ...

  5. javascript 定义类&lpar;转载&rpar;

    Javascript本身并不支持面向对象,它没有访问控制符,它没有定义类的关键字class,它没有支持继承的extend或冒号,它也没有用来支持虚函数的virtual,不过,Javascript是一门 ...

  6. 修改maven的默认JDK

    在我们实际使用IDEA开发maven项目的时候,创建maven项目的默认版本是jdk1.5,当然我们可以通过其他手段去修改module的JDK版本,或JDK的编译级别等等,但是如果每次你都这样修改,那 ...

  7. c语言的作用域、变量与结构体

    一.变量的作用域 根据变量的作用域,可以分为: 1.局部变量: 1> 定义:在函数(代码块)内部定义的变量(包括函数的形参) 2> 作用域:局部变量只有在定义它的函数内部使用,其它函数不能 ...

  8. JAVA中的数据存储空间简述

    在 JAVA 中,有六个不同的地方可以存储数据: 1. 寄存器( register ): 最快的存储区,因为它位于不同于其他存储区——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据需求进 ...

  9. &lowbar;mount&lowbar;vendor

    允许NPC售卖坐骑时进行预览

  10. 学习笔记之Data Visualization

    Data visualization - Wikipedia https://en.wikipedia.org/wiki/Data_visualization Data visualization o ...