POJ 3107 Godfather (树的重心)
Posted leonard-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3107 Godfather (树的重心)相关的知识,希望对你有一定的参考价值。
题目链接: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 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
题解:和上题一样的,注意用链式前向星存图。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 (树的重心)的主要内容,如果未能解决你的问题,请参考以下文章