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

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
 
题意:输出一棵树的所有重心
思路:树形dp。STL超时,用前向星存图。
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<bitset>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int> P;
const int N=1e5+,M=1e5+;
const int inf=0x3f3f3f3f;
const ll INF=1e18+,mod=1e9+;
int n;
vector<int>G[N];
int ans;
int si[N],maxx[N];
struct edge
{
int from,to;
int next;
};
edge es[M];
int cnt,head[N];
void init()
{
cnt=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v)
{
cnt++;
es[cnt].from=u,es[cnt].to=v;
es[cnt].next=head[u];
head[u]=cnt;
}
int dfs(int u,int fa)
{
for(int i=head[u]; i!=-; i=es[i].next)
{
int v=es[i].to;
if(v==fa) continue;
si[u]+=dfs(v,u);
maxx[u]=max(maxx[u],si[v]);
}
si[u]++;
maxx[u]=max(maxx[u],n-si[u]);
ans=min(ans,maxx[u]);
return si[u];
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
for(int i=; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
memset(si,,sizeof(si));
memset(maxx,,sizeof(maxx));
ans=inf;
dfs(,);
int cou=;
for(int i=; i<=n; i++)
{
if(maxx[i]==ans&&!cou) printf("%d",i),cou++;
else if(maxx[i]==ans) printf(" %d",i),cou++;
}
printf("\n");
for(int i=; i<=n+; i++) G[i].clear();
}
return ;
}

前向星存图 树形dp

POJ 3107.Godfather 树形dp的更多相关文章

  1. poj 3107 Godfather 求树的重心【树形dp】

    poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...

  2. POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)

    关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...

  3. # [Poj 3107] Godfather 链式前向星+树的重心

    [Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...

  4. [POJ 1155] TELE (树形dp)

    题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...

  5. Apple Tree POJ - 2486 (树形dp)

    题目链接: D - 树形dp  POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...

  6. Anniversary party POJ - 2342 (树形DP)

    题目链接:  POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...

  7. POJ 3342 (树形DP)

    题意 :给出一些上下级关系,要求i和i的直接上级不能同时出现,现在选出一些人构成一个集合,问你这个集合里面的最大人数是都少,同时给出这个最大的人数的集合是否唯一. 思路:树形DP,dp[i][0],表 ...

  8. POJ 2342 (树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 ...

  9. POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

随机推荐

  1. Linux打开文件设置

    在某些情况下会要求增加Linux的文件打开数,以增加服务器到处理效率,在Linux下查看最多能打开的文件数量为: cat /proc/sys/fs/file-max 然后设置ulimit -n 文件数 ...

  2. 更多FMK 的还是看万一的吧

    http://www.cnblogs.com/del/category/323943.html 记录一下, 作为目录

  3. 秒懂,Java 注解 (Annotation)你可以这样学 - CSDN博客

    https://blog.csdn.net/briblue/article/details/73824058 文章开头先引入一处图片. 这处图片引自老罗的博客.为了避免不必要的麻烦,首先声明我个人比较 ...

  4. Flask--(一对多demo)作者书籍模型

    一对多模型的增加和删除 后端实现: from flask import Flask from flask import flash from flask import redirect from fl ...

  5. 如何从 VSS 迁移到Team Foundation Server

    TFS 2012自带了vss upgrade wizard工具, 在Team Foundation Server管理控制台左侧的最后一个菜单其他工具和组件中,选择Visual SourceSafe升级 ...

  6. maven打包时报错:-source 1.5 中不支持 diamond 运算符

    报错现象: 解决方法: 在pom文件中加入下面依赖 <build> <plugins> <plugin> <groupId>org.apache.mav ...

  7. Metasploit 简单渗透应用

    1.Metasploit端口扫描: 在终端输入msfconsole或直接从应用选metasploit 进入msf>nmap -v -sV 192.168.126.128  与nmap结果一样 用 ...

  8. MDI容器

    MDI容器 具体步骤如下: private void 销售ToolStripMenuItem_Click(object sender, EventArgs e) { VisibledForm(); F ...

  9. Master公式计算递归时间复杂度

    我们在算递归算法的时间复杂度时,Master定理为我们提供了很强大的便利! Master公式在我们的面试编程算法中除了BFPRT算法的复杂度计算不了之外,其他都可以准确计算! 这里用求数组最大值的递归 ...

  10. 关于163发邮件报错535 Error:authentication failed解决方法

    关于发邮件报错535 Error:authentication failed解决方法 调用163邮箱服务器来发送邮件,我们需要开启POP3/SMTP服务,这时163邮件会让我们设置客户端授权码,这个授 ...