POJ3107Godfather(求树的重心裸题)

Posted ---学习ing---

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3107Godfather(求树的重心裸题)相关的知识,希望对你有一定的参考价值。

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

题意:

求所有树的重心,字典序输出。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=100010;
int Laxt[maxn],Next[maxn],To[maxn],cnt;
int son[maxn],sz[maxn];
int q[maxn],tot,n,root;
void add(int u,int v)
{
    Next[++cnt]=Laxt[u];
    Laxt[u]=cnt;
    To[cnt]=v;
}
void dfs(int u,int Pre)
{
    sz[u]=1;
    for(int i=Laxt[u];i;i=Next[i]){
        int v=To[i];
        if(v!=Pre) {
            dfs(v,u);
            sz[u]+=sz[v];
            son[u]=max(sz[v],son[u]);
        }
    }
    son[u]=max(son[u],n-sz[u]);
    if(son[u]<son[root]){
        root=u;tot=0;q[++tot]=u;
    }
    else if(son[u]==son[root])  q[++tot]=u;
}
int main()
{
    while(~scanf("%d",&n)){
        memset(son,0,sizeof(son));
        memset(sz,0,sizeof(sz));
        memset(Laxt,0,sizeof(Laxt));
        int u,v; tot=cnt=0;
        for(int i=1;i<n;i++){
            scanf("%d%d",&u,&v);
            add(u,v); add(v,u);
        }              
        root=0;son[root]=0x7fffffff;
        dfs(1,0); 
        sort(q+1,q+tot+1);
        printf("%d",q[1]);  
        for(int i=2;i<=tot;i++) 
          printf(" %d",q[i]); 
        printf("\n");
    }
    return 0;
}

 

以上是关于POJ3107Godfather(求树的重心裸题)的主要内容,如果未能解决你的问题,请参考以下文章

poj3107 Godfather 求树的重心

poj 3107 Godfather 求树的重心树形dp

Godfather(POJ3107)——求树的重心(递归)

POJ 3107 Godfather (树的重心)

poj3107 Godfather (树的重心)

poj3107Godfather(树的重心)