BNUOJ 3226 Godfather

Godfather

Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on  PKU. Original ID: 3107
64-bit integer IO format: %lld      Java class name: Main
 

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

 
解题:树形dp。。。vector不能用,一用就超时,mother's egg!把某一点去掉以后,剩下什么呢?它的所有子树,以及它的上部分。上部分的节点个数如何计算?树的总数减去当前节点为根的树的节点数目就是上部分的树的节点数 ,其子树的节点数递归的时候已经计算出来了,求子树的节点个数和上部分的节点个数的最大数就是dp[u]了。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define INF 0x3f3f3f3f
15 using namespace std;
16 const int maxn = 50010;
17 struct arc{
18     int to,next;
19 };
20 int num[maxn],n,dp[maxn];
21 int head[maxn],tot,ans;
22 arc g[maxn<<2];
23 void add(int u,int v){
24     g[tot].to = v;
25     g[tot].next = head[u];
26     head[u] = tot++;
27 }
28 void dfs(int u,int fa){
29     num[u] = 1;
30     dp[u] = 0;
31     for(int i = head[u]; i != -1; i = g[i].next){
32         if(g[i].to == fa) continue;
33         dfs(g[i].to,u);
34         num[u] += num[g[i].to];
35         dp[u] = max(dp[u],num[g[i].to]);
36     }
37     dp[u] = max(dp[u],n-num[u]);
38     ans = min(ans,dp[u]);
39 }
40 int main() {
41     int i,u,v;
42     bool flag;
43     while(~scanf("%d",&n)){
44         memset(head,-1,sizeof(head));
45         tot = 0;
46         for(i = 1; i < n; i++){
47             scanf("%d %d",&u,&v);
48             add(u,v);
49             add(v,u);
50         }
51         ans = INF;
52         dfs(1,-1);
53         flag = true;
54         for(i = 1; i <= n; i++){
55             if(dp[i] == ans){
56                 if(flag) {flag = false;printf("%d",i);}
57                 else printf(" %d",i);
58             }
59         }
60         puts("");
61     }
62     return 0;
63 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/3905186.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值