码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3107 Godfather (树的重心)

时间:2018-04-21 13:28:51      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:follow   tee   bool   back   oid   input   who   SM   fat   

题目链接:http://poj.org/problem?id=3107

题目:

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

题解:和上题一样的,注意用链式前向星存图。vector要超时。

 1 #include <vector>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int N=1e5;
 8 const int INF=0x3f3f3f3f;
 9 int dp[N],ans[N],n,cnt,res=INF;
10 int head[N];
11 bool vis[N];
12 
13 struct Edge{
14     int to,next;
15 };
16 
17 Edge edge[N];
18 
19 void add(int u,int v){
20     edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt++;
21     edge[cnt].to=u;edge[cnt].next=head[v];head[v]=cnt++;
22 }
23 
24 void DFS(int u){
25     dp[u]=1;
26     vis[u]=1;
27     int mx=0;
28     for(int i=head[u];~i;i=edge[i].next){
29         int v=edge[i].to;
30         if(vis[v]) continue;
31         DFS(v);
32         dp[u]+=dp[v];
33         mx=max(mx,dp[v]);
34     }
35     mx=max(mx,n-dp[u]);
36     ans[u]=mx;
37     if(mx<res) res=mx;
38 }
39 
40 int main(){
41     memset(head,-1,sizeof(head));
42     scanf("%d",&n);
43     for(int i=1;i<n;i++){
44         int u,v;
45         scanf("%d%d",&u,&v);
46         add(u,v);
47     }
48     DFS(1);
49     vector <int> p;
50     for(int i=1;i<=n;i++){
51         if(ans[i]==res) p.push_back(i);
52     }
53     for(int i=0;i<p.size();i++){
54         if(i==0) printf("%d",p[i]);
55         else printf(" %d",p[i]);
56     }
57     return 0;
58 }

 

POJ 3107 Godfather (树的重心)

标签:follow   tee   bool   back   oid   input   who   SM   fat   

原文地址:https://www.cnblogs.com/Leonard-/p/8900641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!