POJ3107Godfather[树形DP 树的重心]
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 6121 | Accepted: 2164 |
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
树的重心裸题
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
struct edge{
int v,ne;
}e[N<<];
int h[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
} int n,u,v;
int d[N],ans[N],num=,mx=1e9;
void dp(int u,int fa){//printf("dp %d %d\n",u,fa);
d[u]=;
int tmp=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(v==fa) continue;
dp(v,u);
d[u]+=d[v];
tmp=max(tmp,d[v]);
}
tmp=max(tmp,n-d[u]);
if(tmp<mx){mx=tmp;num=;ans[++num]=u;}
else if(tmp==mx){ans[++num]=u;}
//printf("%d %d\n",u,d[u]);
}
int main(){
n=read();
for(int i=;i<=n-;i++){u=read();v=read();ins(u,v);}
dp(,);
sort(ans+,ans++num);
for(int i=;i<=num;i++) printf("%d ",ans[i]);
//cout<<"num"<<num;
}
POJ3107Godfather[树形DP 树的重心]的更多相关文章
- POJ 1655.Balancing Act 树形dp 树的重心
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14550 Accepted: 6173 De ...
- 树形dp&&树的重心(D - Godfather POJ - 3107)
题目链接:https://cn.vjudge.net/contest/277955#problem/D 题目大意:求树的重心(树的重心指的是树上的某一个点,删掉之后形成的多棵树中节点数最大值最小). ...
- POJ 2378.Tree Cutting 树形dp 树的重心
Tree Cutting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4834 Accepted: 2958 Desc ...
- hdu-4118 Holiday's Accommodation(树形dp+树的重心)
题目链接: Holiday's Accommodation Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 200000/200000 ...
- poj1655(dfs,树形dp,树的重心)(点分治基础)
题意:就是裸的求树的重心. #include<cstring> #include<algorithm> #include<cmath> #include<cs ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- [poj3107]Godfather_树形dp_树的重心
Godfather poj-3107 题目大意:求树的重心裸题. 注释:n<=50000. 想法:我们尝试用树形dp求树的重心,关于树的重心的定义在题目中给的很明确.关于这道题,我们邻接矩阵存不 ...
- POJ 3162.Walking Race 树形dp 树的直径
Walking Race Time Limit: 10000MS Memory Limit: 131072K Total Submissions: 4123 Accepted: 1029 Ca ...
随机推荐
- Asp.net Ibatis 增、删、改、查
好久都没用.net Ibatis配置了 今天给大家分享一下获取它的增.删.改.查. #region 节点类型表 public bool InsertNodeType(NodeType allRevie ...
- ionic + cordova 配置和开发过程中的一些问题
1.Android sdk:ERROR: SWT folder '' does not exist.问题 在win x86系统中,如果运行Android的 sdk manager,会出现Android ...
- 总结CSS3新特性(Animation篇)
动画(Animation),是CSS3的亮点.//之一 通过animation属性指定@keyframe来完成关键帧动画; @keyframe用法: @keyframes name { 0% { to ...
- SAP ALV内嵌(In-place)Excel的问与答
1.问题:点击ALV工具栏的"Excel"图标后,出现空白的内嵌Excel界面,无法正常显示报表数据.可按以下思路解决:(1)检查Excel中的宏安全设置选项.访问方法:启动Exc ...
- WAF指纹识别和XSS过滤器绕过技巧
[译文] -- “Modern Web Application Firewalls Fingerprinting and Bypassing XSS Filters” 0x1 前言 之前在乌云drop ...
- Autodesk View and Data API练练手
大家如果参加过我们的活动,你应该已经听过看过不少关于View and Data Web Service的例子里,如果还没有的话,请看看下面这几篇: http://www.cnblogs.com/jun ...
- Android 查看手机中所有进程
真机测试的时候发现DDMS对进程的显示很不给力,一些进程管理工具又不显示包名. 所以就自己写了一个小程序,查看自己手机中的进程,显示当前时间和进程的包名: 程序运行截图: 布局: <Linear ...
- 遍历collection
链接 遍历NSArray 正向遍历 for (id object in array) 反向遍历 for (id object in [array reverseObjectEnumerator]) 如 ...
- HTTPS学习总结
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 21.0px Verdana; color: #393939 } span.s1 { } HTTPS学习总结 ...
- Android 手机卫士--home界面布局
本文实现当从splash界面进入hone界面的时候,产生一种渐进淡入的动画效果,在onCreate中调用一个方法initAnimation(),代码如下: /** * 添加淡入的动画效果 */ pri ...