Godfather(poj 3107 树形DP入门)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11568 | Accepted: 4045 |
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
Source
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 6 using namespace std; 7 8 int n; 9 const int maxn=5e4+5; 10 int fa[maxn],sz[maxn],arr[maxn]; 11 12 struct Edge{ 13 int v,next; 14 }edge[maxn<<1]; 15 int cnt,head[maxn]; 16 void add(int u,int v){ //链式前向星 17 edge[cnt].v=v; 18 edge[cnt].next=head[u]; 19 head[u]=cnt++; 20 } 21 void init(){ memset(head,-1,sizeof(head)); cnt=0; } 22 23 int dfs(int u,int pre){ 24 fa[u]=pre; 25 sz[u]=1; 26 for(int i=head[u];i!=-1;i=edge[i].next){ 27 int x=edge[i].v; 28 if(x!=pre){ 29 sz[u]+=dfs(x,u); 30 } 31 } 32 return sz[u]; 33 } 34 35 int main() 36 { 37 scanf("%d",&n); 38 init(); 39 for(int i=1,d1,d2;i<=n-1;i++){ 40 scanf("%d%d",&d1,&d2); 41 add(d1,d2); 42 add(d2,d1); //把边加进去 43 } 44 int zhi=dfs(1,0); //根节点的父节点为0; 45 int minn=0x3f3f3f3f; 46 47 for(int i=1;i<=n;i++){ 48 int big=n-sz[i]; 49 for(int j=head[i];j!=-1;j=edge[j].next){ 50 int x=edge[j].v; 51 if(x!=fa[i]){ 52 big=max(big,sz[x]); 53 } 54 } 55 arr[i]=big; 56 if(big<minn) minn=big; 57 } 58 // cout << flag << endl; 59 int biaoji=0; 60 for(int i=1;i<=n;i++){ 61 if(arr[i]==minn){ 62 if(biaoji) printf(" "); 63 printf("%d",i); 64 biaoji=1; 65 } 66 } 67 return 0; 68 }
原文地址:https://www.cnblogs.com/qq-1585047819/p/11940392.html
- PHP页面跳转代码
- 分布式事务 TCC-Transaction 源码解析 —— 调试环境搭建
- 机器学习入门——使用python进行监督学习
- 推荐算法的介绍,第一部分——协同过滤与奇异值分解
- 在ASP中实现UNIX时间戳
- 【学术】厉害了我的哥,国外技术大咖仿造了谷歌的Arts &Culture,找到古代的“你”
- 【技巧】应赛技巧,教你如何在Kaggle比赛中排在前1%
- 熔断器 Hystrix 源码解析 —— 命令执行(一)之正常执行逻辑
- 智能主题检测与无监督机器学习:识别颜色教程
- 如何下载安装Weka机器学习工作平台
- Dubbo 源码解析 —— LoadBalance
- 如何处理机器学习中类的不平衡问题
- 【死磕Java并发】—– Java内存模型之重排序
- Mask R-CNN源代码终于来了,还有它背后的物体检测平台
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法