Pku3107 Godfather

来源:互联网 发布:电视机顶盒软件 编辑:程序博客网 时间:2024/06/05 19:44

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 ai, bi 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

HINT
tree dp

题意简述
给一颗n个结点的树,节点编号为1~n,问删除一个节点之后,让剩下的分支中节点数量最多的尽量少,可能有多种方案,按编号顺序输出。

题解
三倍经验题(另外两道请戳这里和这里)
简单的tree dp
我用的是邻接表存树的结构

代码

#include <cstdio>#include <algorithm>const int maxn=50000;const int inf=2147483647;int n,i,x,y,tot,fl;int size[maxn+10],pre[maxn*2+10],now[maxn+10],son[maxn*2+10],z[maxn+10];int f[maxn+10][2],g[maxn+10][2];int maxlist[maxn+10],nummax,nowmax;int dfs(int u,int fa){    int maxx=-1;    size[u]=1;    int j=now[u];    while (j!=0)    {        if (son[j]==fa)        {            j=pre[j];            continue;        }        dfs(son[j],u);          size[u]+=size[son[j]];        if (size[son[j]]>maxx)        {            maxx=size[son[j]];        }        j=pre[j];    }    if (n-size[u]>maxx)    {        maxx=n-size[u];    }    if (maxx==nowmax)    {        nummax++;        maxlist[nummax]=u;    }    if (maxx<nowmax)    {        nowmax=maxx;        nummax=1;        maxlist[nummax]=u;    }    return size[u];}int main(){    nowmax=inf;    scanf("%d",&n);    for (i=1;i<=n-1;i++)    {        scanf("%d%d",&x,&y);        tot++;        pre[tot]=now[x];        now[x]=tot;        son[tot]=y;        tot++;        pre[tot]=now[y];        now[y]=tot;        son[tot]=x;    }    dfs(1,0);    std::sort(maxlist+1,maxlist+nummax+1);    for (i=1;i<nummax;i++)    {        printf("%d ",maxlist[i]);    }    printf("%d",maxlist[nummax]);    return 0;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 有哪些性价比高的手机 笔记本那款性价比高 哪种纸尿裤性价比高 标致哪款车性价比高 1000性价比高的手机 行车记录仪性价比高的品牌 现在性价比比较高的手机 性价比高的紧凑型suv排行榜 性价比高的平板推荐 性比价是什么意思 性价 哪个牌子的表性价比高 什么马桶好用性价比高 电脑哪个牌子性价比高 婴儿推车哪个性价比高 哪个牌子电脑性价比高 三星哪款手机性价比高 现在那些手机性价比高 哪个品牌空调性价比高 哪个牌子的拉杆箱性价比高 瓷砖哪个牌子的性价比比较好 什么牌子的羽毛球拍性价比高 什么牌子的蓝牙耳机性价比高 男士钱包什么牌子性价比高 抽油烟机哪个牌子性价比高 液晶电视什么牌子性价比高 哪个牌子的油烟机性价比高 森海塞尔哪款耳机性价比高 哪个牌子的平板性价比高 蓝牙耳机什么牌子性价比高 什么牌子的充电宝性价比高 哪个牌子的按摩椅性价比高 机械键盘什么牌子性价比高 什么牌子台式电脑性价比高 哪个牌子的电动车性价比高 什么牌子的机械键盘性价比高 跑步机哪个牌子的性价比高 现在买什么电视性价比高 哪个牌子的粉底液性价比高 thinkpad哪个系列性价比高 什么牌子的平板电脑性价比高