[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. NSObject class和NSObject protocol的关系(抽象基类与协议)

    [转载请注明出处] 1.接口的实现 对于接口这一概念的支持,不同语言的实现形式不同.Java中,由于不支持多重继承,因此提供了一个Interface关键词.而在C++中,通常是通过定义抽象基类的方式来 ...

  2. skb管理函数之skb_clone、pskb_copy、skb_copy

    skb_clone--只复制skb描述符本身,如果只修改skb描述符则使用该函数克隆: pskb_copy--复制skb描述符+线性数据区域(包括skb_shared_info),如果需要修改描述符以 ...

  3. Linux NAPI/非NAPI

    本文主要介绍二层收包流程,包括NAPI与非NAPI方式: NAPI:数据包到来,第一个数据包产生硬件中断,中断处理程序将设备的napi_struct结构挂在当前cpu的待收包设备链表softnet_d ...

  4. 网络知识===wireshark抓包出现“TCP segment of a reassembled PDU”的解释(载)

    网上胡说八道,众说风云,感觉这篇还算靠谱点. 原文链接:http://blog.csdn.net/dog250/article/details/51809566 为什么大家看到这个以后总是会往MSS, ...

  5. 在linux下有没有什么软件可以连接windows上的MSSQL SERVER

    在linux下有没有什么软件可以连接windows上的MSSQL SERVER GUI的http://dbeaver.jkiss.org/ http://bbs.csdn.net/topics/391 ...

  6. 看jquery3.3.1学js类型判断的技巧

    需要预习:call , typeof, js数据类型 1. isFunction中typeof的不靠谱 源码: var isFunction = function isFunction( obj ) ...

  7. HighGUI图形图像界面初步——鼠标操作

    OpenCV中的鼠标操作和滑动条的消息映射方式很类似,都是通过一个中介函数配合一个回调函数来实现的,创建和指定滑动条回调函数为createTrackbar, 而指定鼠标操作消息回调函数的函数为setM ...

  8. Linux Python apache的cgi配置

    一.找到安装Apache的目录/usr/local/apache2/conf,并对httpd.conf配置文件进行修改 1.加载cgi模块 去掉注释: LoadModule cgid_module m ...

  9. Mybatis插入数据返回自增主键

    方法有很多,参考 mysql函数之六:mysql插入数据后返回自增ID的方法,last_insert_id(),selectkey 这里记录一下工作中自己用到的selectkey方法的详细过程. po ...

  10. hdu 1430(BFS+康托展开+映射+输出路径)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...