poj3107--Godfather(树形DP练习3)

本文介绍了一种通过分析已知黑帮成员间的通讯关系来识别黑帮头目的算法。该算法利用图论中的树结构,通过计算每个成员作为教父时剩余最大连接组件的大小来确定最有可能的教父人选。
Godfather
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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


给出黑帮的关系图(无向图),找出黑帮教父,要求将黑帮教父逮捕后,剩余的连接图最小。

先建无向图,vis标记,使得每个点只被访问一次。

dp[i][0] 节点i控制的子树的大小(包含i)

dp[i][1]将i去除后,剩下的连接图的最大值。

将i去除后,剩下的连接图由几部分组成,各子树形成的图和父节点连接的图。

i为j的父节点时。

dp[i][0] = dp[j][0] + 1 ;

dp[i][1] = max( n-dp[i][0],dp[j][0] ) ;



#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std ;
struct node{
    int u , v ;
    int next ;
}edge[120000];
int head[60000] , cnt ;
int vis[60000] ;
int dp[60000][2] ;//dp[i][0]代表i能控制的人数,dp[i][1] i逮捕后,最大的团伙
queue <int> que ;
void add(int u,int v)
{
    edge[cnt].u = u ; edge[cnt].v = v ;
    edge[cnt].next = head[u] ;
    head[u] = cnt++ ;
    edge[cnt].u = v ; edge[cnt].v = u ;
    edge[cnt].next = head[v] ;
    head[v] = cnt++ ;
    return ;
}
void dfs(int u)
{
    int i , v ;
    dp[u][0] = 1 ;
    for( i = head[u] ; i != -1 ; i = edge[i].next)
    {
        v = edge[i].v ;
        if( vis[v] ) continue ;
        vis[v] = 1 ;
        dfs(v) ;
        dp[u][0] += dp[v][0] ;
    }
    return ;
}
int main()
{
    int n , u , v , i , min1 , num ;
    while( scanf("%d", &n) != EOF )
    {
        cnt = num = 0 ;
        min1 = n+1 ;
        memset(vis,0,sizeof(vis)) ;
        memset(head,-1,sizeof(head)) ;
        memset(dp,0,sizeof(dp)) ;
        for(i = 1 ; i < n ; i++)
        {
            scanf("%d %d", &u, &v) ;
            add(u,v) ;
        }
        vis[1] = 1 ;
        dfs(1) ;
        memset(vis,0,sizeof(vis)) ;
        while( !que.empty() ) que.pop() ;
        que.push(1) ;
        vis[1] = 1 ;
        while( !que.empty() )
        {
            u = que.front() ;
            que.pop() ;
            for(i = head[u];  i != -1 ; i = edge[i].next)
            {
                v = edge[i].v ;
                if( vis[v] ) continue ;
                dp[u][1] = max( dp[u][1],dp[v][0] ) ;
                dp[v][1] = n - dp[v][0] ;
                vis[v] = 1 ;
                que.push(v) ;
            }
        }
        for(i = 1 ; i <= n ; i++)
        {
            if( dp[i][1] < min1 )
            {
                min1 = dp[i][1] ;
                num = 1 ;
            }
            else if( dp[i][1] == min1 )
            {
                num++ ;
            }
        }
        for(i = 1 ; i <= n ; i++)
        {
            if( dp[i][1] == min1 )
            {
                printf("%d", i) ;
                num-- ;
                if( num )
                    printf(" ") ;
                else
                    printf("\n") ;
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值