POJ 3107 树形dp

来源:互联网 发布:照片加背景音乐软件 编辑:程序博客网 时间:2024/05/16 14:50
Godfather
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 4412 Accepted: 1473

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 aren 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 gangsterbi. 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

61 22 32 53 43 6

Sample Output

2 3题意:求出去掉一个点后分离出的联通块点数最大的最小值的所有的点,在树形dp的时候记录一下代码:
/* ***********************************************Author :xianxingwuguanCreated Time :2014-2-4 10:07:04File Name :1.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;const int maxn=100010;int head[maxn],tol,dp[maxn],res,cnt[maxn],n;struct node{int next,to;}edge[3*maxn];void add(int u,int v){edge[tol].to=v;edge[tol].next=head[u];head[u]=tol++;}void dfs(int u,int fa){cnt[u]=1;dp[u]=0;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(v==fa)continue;dfs(v,u);cnt[u]+=cnt[v];dp[u]=max(dp[u],cnt[v]);}dp[u]=max(dp[u],n-cnt[u]);res=min(res,dp[u]);}int main(){     //freopen("data.in","r",stdin);     //freopen("data.out","w",stdout);     int i,j,k,m; while(~scanf("%d",&n)){ memset(head,-1,sizeof(head)); tol=0; for(i=1;i<n;i++){ scanf("%d%d",&j,&k); add(j,k); add(k,j); } res=INF; dfs(1,-1);         for(i=1;i<=n;i++) if(dp[i]==res){ printf("%d",i); break; } for(j=i+1;j<=n;j++) if(dp[j]==res) cout<<" "<<j; puts(""); }     return 0;}


0 0
原创粉丝点击