[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. Zabbix 通过 JMX 监控 java 进程

    参考: [ JMX monitoring ] [ Zabbix Java gateway ] [ JMX Monitoring (Java Gateway) not Working ] [ Monit ...

  2. MSSQL备份脚本

    ) ) ) ),),':',''),' ',''),'-',''),'.','') set @name=N'DEMO'+@temp+'-完整 数据库 备份' set @disk=N'F:\Backup ...

  3. jQuery右侧悬浮楼层滚动 电梯菜单

    http://www.kaiu.net/effectCon.aspx?id=2198 <!doctype html> <html> <head> <meta ...

  4. 工具_HBuilder使用快捷方式

    HBuilder常用快捷键大概共9类([4 13 3]文件.编辑.插入:[4 9 8]选择.跳转.查找:[1 1 6]运行.工具.视图) 1.文件(4) 新建 Ctrl + N 关闭 Ctrl + F ...

  5. 如何入门 Python 爬虫?

    作者:谢科   来源:知乎链接:https://www.zhihu.com/question/20899988/answer/24923424 著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  6. https 多路复用的理解~转载

    https://segmentfault.com/q/1010000005167289 这里面的http/2连接是指tcp/ip层的连接还是http应用层(也就是我们平常在chrome F12 net ...

  7. Linux配置Tomcat

    系统:Ubuntu,Tomcat:apache-tomcat-8.5.23.tar.gz 1,找到apache-tomcat-8.5.23.tar.gz,复制到 /usr/local root@ubu ...

  8. dockerfile实例--安装nginx

    [root@localhost ~]# vi Dockerfile //ADD FROM centos_with_net MAINTAINER frankie onez0714@.com RUN yu ...

  9. git配置服务器版仓库

    1.git 可以使用四种主要的协议来传输数据:本地传输,SSH 协议,Git 协议和 HTTP 协议.现在使用360同步盘同步一个网络的仓库管理. 2.查看设置好的360同步盘的文件 3.创建空的仓库 ...

  10. Nginx惊群处理

    惊群:是指在多线程/多进程中,当有一个客户端发生链接请求时,多线程/多进程都被唤醒,然后只仅仅有一个进程/线程处理成功,其他进程/线程还是回到睡眠状态,这种现象就是惊群. 惊群是经常发生现在serve ...