码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-18 11:54:24      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

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;
}


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

标签:

原文地址:http://blog.csdn.net/winddreams/article/details/42834045

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!