Godfather(poj 3107 树形DP入门)

Godfather
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11568   Accepted: 4045

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 ai, bi 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
 
树的重心的概念:   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    树上一个节点满足其所有的子树种最大的紫薯节点数最少 ,那么这个点就是这颗树的重心。
 
用vector会超时  改用链式前向星建图
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 int n;
 9 const int maxn=5e4+5;
10 int fa[maxn],sz[maxn],arr[maxn];
11 
12 struct Edge{
13     int v,next;
14 }edge[maxn<<1];
15 int cnt,head[maxn];
16 void add(int u,int v){   //链式前向星
17     edge[cnt].v=v;
18     edge[cnt].next=head[u];
19     head[u]=cnt++;
20 }
21 void init(){ memset(head,-1,sizeof(head)); cnt=0; }
22 
23 int dfs(int u,int pre){
24     fa[u]=pre;
25     sz[u]=1;
26     for(int i=head[u];i!=-1;i=edge[i].next){
27         int x=edge[i].v;
28         if(x!=pre){
29             sz[u]+=dfs(x,u);
30         }
31     }
32     return sz[u];
33 }
34 
35 int main()
36 {
37     scanf("%d",&n);
38     init();
39     for(int i=1,d1,d2;i<=n-1;i++){
40         scanf("%d%d",&d1,&d2);
41         add(d1,d2);
42         add(d2,d1);  //把边加进去
43     }
44     int zhi=dfs(1,0);   //根节点的父节点为0;
45     int minn=0x3f3f3f3f;
46 
47     for(int i=1;i<=n;i++){
48         int big=n-sz[i];
49         for(int j=head[i];j!=-1;j=edge[j].next){
50             int x=edge[j].v;
51             if(x!=fa[i]){
52                 big=max(big,sz[x]);
53             }
54         }
55         arr[i]=big;
56         if(big<minn) minn=big;
57     }
58 //    cout << flag << endl;
59     int biaoji=0;
60     for(int i=1;i<=n;i++){
61         if(arr[i]==minn){
62             if(biaoji) printf(" ");
63             printf("%d",i);
64             biaoji=1;
65         }
66     }
67     return 0;
68 }
View Code

 

posted @ 2019-11-27 10:41  厂长在线养猪  Views(131)  Comments(0Edit  收藏  举报