日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > poj 3107 Godfather (樹形dp)

poj 3107 Godfather (樹形dp)

來源:程序員人生   發布時間:2015-07-29 07:57:57 閱讀次數:3961次

Godfather
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5064   Accepted: 1769

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

刪點,使剩下的分支中最大的節點數最小。

進行深搜,找到每一個節點的子節點構成子樹的節點數目,比較得到答案


#include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<queue> #include<vector> using namespace std; #define ll long long #define N 51000 #define mem(a,t) memset(a,t,sizeof(a)) vector<int>g[N]; int ans[N]; struct ege { int v,next; }e[N*2]; int head[N]; int cnt,n,n1,mmin; int num[N]; //記錄i子樹包括節點數 void add(int u,int v) { e[cnt].v=v; e[cnt].next=head[u]; head[u]=cnt++; } int dfs(int u,int fa) { int i,v,tmp=0,t1; num[u]=1; //求當前樹枝U的子節點和本身的個數和 for(i=head[u];i!=⑴;i=e[i].next) { v=e[i].v; if(v!=fa) { num[u]+=dfs(v,u); tmp=max(tmp,num[v]); } } t1=max(tmp,n-num[u]); //去掉點u后,最大樹的節點數目 if(t1<mmin) { mmin=t1; n1=0; ans[n1++]=u; } else if(t1==mmin) { ans[n1++]=u; } return num[u]; } int main() { //freopen("in.txt","r",stdin); int i,j,u,v; while(~scanf("%d",&n)) { mem(head,⑴); cnt=0; for(i=1;i<n;i++) { scanf("%d%d",&u,&v); add(u,v); add(v,u); } mem(num,0); //num[i]記錄節點i的子樹點個數 n1=0; mmin=n; dfs(1,⑴); sort(ans,ans+n1); for(i=0;i<n1;i++) printf("%d%c",ans[i],(i==n1⑴)?' ':' '); } return 0; }







生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 午夜精品久久久久久99热软件 | 美女久久 | 最新中文字幕视频在线 | 中文日产幕无线码一区二区 | 亚洲精品一区二区三 | 日韩成人在线电影 | 欧美日韩在线观看中文字幕 | 亚洲欧美一区二区在线观看 | 国产精品福利在线 | 亚洲精品电影在线 | 国产日产久久高清欧美一区 | 国产欧美精品一区二区三区四区 | 久久久久久成人精品 | 久久免费99精品久久久久久 | 一区二区三区 在线 | 538国产精品视频一区二区 | 好吊色在线 | 日韩精品久久久 | 国产在线小视频 | 91精品国产91久久综合 | 日韩一区二区三区精品 | www.色网| 国产精品卡一卡二 | 欧美日韩一区二区三区视频 | 国产精品传媒麻豆hd | 精品国产欧美一区二区三区成人 | 亚洲国产精品一区二区第一页 | 美日韩精品 | 日韩午夜在线电影 | 亚洲日本久久 | 欧美亚一区二区 | 毛片特黄 | 欧美精品一区二区三区在线播放 | 久久99久久久 | 日本国产欧美 | 欧美色欧美亚洲另类二区 | 中文字幕天堂 | 国产伊人网 | 久久久久久久久久久国产 | 综合久久伊人 | 亚洲精品免费网站 |