POJ 3107.Godfather 树形dp
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 7536 | Accepted: 2659 |
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<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<bitset>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int> P;
const int N=1e5+,M=1e5+;
const int inf=0x3f3f3f3f;
const ll INF=1e18+,mod=1e9+;
int n;
vector<int>G[N];
int ans;
int si[N],maxx[N];
struct edge
{
int from,to;
int next;
};
edge es[M];
int cnt,head[N];
void init()
{
cnt=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v)
{
cnt++;
es[cnt].from=u,es[cnt].to=v;
es[cnt].next=head[u];
head[u]=cnt;
}
int dfs(int u,int fa)
{
for(int i=head[u]; i!=-; i=es[i].next)
{
int v=es[i].to;
if(v==fa) continue;
si[u]+=dfs(v,u);
maxx[u]=max(maxx[u],si[v]);
}
si[u]++;
maxx[u]=max(maxx[u],n-si[u]);
ans=min(ans,maxx[u]);
return si[u];
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
for(int i=; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
memset(si,,sizeof(si));
memset(maxx,,sizeof(maxx));
ans=inf;
dfs(,);
int cou=;
for(int i=; i<=n; i++)
{
if(maxx[i]==ans&&!cou) printf("%d",i),cou++;
else if(maxx[i]==ans) printf(" %d",i),cou++;
}
printf("\n");
for(int i=; i<=n+; i++) G[i].clear();
}
return ;
}
前向星存图 树形dp
POJ 3107.Godfather 树形dp的更多相关文章
- poj 3107 Godfather 求树的重心【树形dp】
poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
- # [Poj 3107] Godfather 链式前向星+树的重心
[Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...
- [POJ 1155] TELE (树形dp)
题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...
- Apple Tree POJ - 2486 (树形dp)
题目链接: D - 树形dp POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...
- Anniversary party POJ - 2342 (树形DP)
题目链接: POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...
- POJ 3342 (树形DP)
题意 :给出一些上下级关系,要求i和i的直接上级不能同时出现,现在选出一些人构成一个集合,问你这个集合里面的最大人数是都少,同时给出这个最大的人数的集合是否唯一. 思路:树形DP,dp[i][0],表 ...
- POJ 2342 (树形DP)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3863 Accepted: 2172 ...
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
随机推荐
- ubuntu命令行打开网页
在Ubuntu下,当需要打开其他格式文件时,比如pdf.jpg.mp3等格式文件,通常做法是进入到文件所在的目录,双击打开,很影响效率.事实上,可以通过命令xdg-open打开这些格式文件,甚至是网页 ...
- 【OpenStack】相关概念
网络 network和subnet Service subnets: 创建network,subnet, instances 官方示例 Network components: Switches/ Ro ...
- 数据库SQL语言学习----左外连接,右外连接,外连接,自然连接的形象对比
现在有两张表,一张Student 另一张Score 1.查询每个学生及其选修课程的情况: 自然连接,Sno在Cscore中找不到就不显示,Cno在Cscore中找不到也不显示 SELECT Stu ...
- 随机森林RandomForest
ID3,C4.5决策树的生成: 输入:训练集D,特征集A,阈值eps, 输出:决策树T 若D中所有样本属于同一类Ck,则T为单节点树,将类Ck作为该结点的类标记,返回T: 若A为空集,即没有特征作为划 ...
- Mike Piehl
some books were writen by him: new about this man website: http://paperstreetenterprises.com
- log4j.properties详解
首先建立项目 package a; import org.apache.log4j.Logger; public class Test { private static Logger logger=L ...
- python 处理 https链接 socket报错 链接https
// socket 链接 https 有问题 得去看看ssl文档 用法 import socketimport ssl def https_test(url): proto = "http& ...
- leetcode208
class TrieNode { public: // Initialize your data structure here. TrieNode() { words=; prefixs=; ;i&l ...
- leetcode771
int numJewelsInStones(string J, string S) { set<char> st; ; for (auto c : J) { st.insert(c); } ...
- 利用python的requests发送http请求
>>> from requests import put, get >>> put('http://localhost:5000/todo1', data={'da ...