poj3107--Godfather(树形DP练习3)
来源:互联网 发布:在联通云数据工作好吗 编辑:程序博客网 时间:2024/06/07 18:19
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
61 22 32 53 43 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)
- [POJ3107]Godfather(树形dp)
- 【树形DP+前向星】 poj3107 Godfather
- POJ3107-树的重心&树形DP-Godfather
- poj3107(树形dp)
- poj3107 Godfather(鬼题畜译)
- poj 3107 Godfather(树形dp)
- POJ 3107 Godfather(树形DP)
- POJ 3107 Godfather(树形DP)
- poj 3107 Godfather(树形DP)
- poj 3107 Godfather(树形dp)
- Godfather (树形dp,树的重心)
- Godfather (树形dp + 树的重心)
- poj3107 Godfather(树的重心)
- POJ3107-God father(树形dp??)
- poj3107、2378、1655(树形DP,割点)
- POJ3107——Godfather
- 【树形dp】PKU-3107-Godfather
- Linux下 Mini-shell的实现(C/C++)
- Managed Metadata Service Application(一)创建Managed Metadata Service Application
- zinnia下user各自的category
- BJFU-ACM-1009 抽奖
- Java – Convert String To Enum Object
- poj3107--Godfather(树形DP练习3)
- 【C语言】01-类型总结
- WV.10-动态链表-搜索某值存在个数
- 机器学习基础(三)Logistic回归模型
- 二叉树的宽度和深度
- 实现方式二:隐藏TabWidget,通过RadioGroup和RadioButton实现底部菜单栏
- 帮你理清Java中字符与字节的编码关系
- hiho-最长回文子串
- 成都芮嘉科技有限公司 招人啦