快速切题 sgu134.Centroid 树形dp
134. Centroid
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
You are given an undirected connected graph, with N vertices and N-1 edges (a tree). You must find the centroid(s) of the tree.
In order to define the centroid, some integer value will be assosciated to every vertex. Let's consider the vertex k. If we remove the vertex k from the tree (along with its adjacent edges), the remaining graph will have only N-1 vertices and may be composed of more than one connected components. Each of these components is (obviously) a tree. The value associated to vertex k is the largest number of vertices contained by some connected component in the remaining graph, after the removal of vertex k. All the vertices for which the associated value is minimum are considered centroids.
Input
The first line of the input contains the integer number N (1<=N<=16 000). The next N-1 lines will contain two integers, a and b, separated by blanks, meaning that there exists an edge between vertex a and vertex b.
Output
You should print two lines. The first line should contain the minimum value associated to the centroid(s) and the number of centroids. The second line should contain the list of vertices which are centroids, sorted in ascending order.
Sample Input
7
1 2
2 3
2 4
1 5
5 6
6 7
Sample Output
3 1
1
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=16005;
const int maxm=2*maxn;
int first[maxn];
int nxt[maxm];
int to[maxm];
int maxson[maxn];
int subtree[maxn];
int n;
int dfs(int s,int f){
int sum=0;
for(int p=first[s];p!=-1;p=nxt[p]){
if(to[p]==f)continue;
int subson=dfs(to[p],s);
maxson[s]=max(maxson[s],subson);
sum+=subson;
}
maxson[s]=max(maxson[s],n-sum-1);
return subtree[s]=sum+1;
}
void addedge(int f,int t,int i){
nxt[2*i]=first[f];
first[f]=2*i;
to[2*i]=t;
nxt[2*i+1]=first[t];
first[t]=2*i+1;
to[2*i+1]=f;
}
int heap[maxn];
int main(){
scanf("%d",&n);
memset(first,-1,sizeof(first));
for(int i=1;i<n;i++){
int f,t;
scanf("%d%d",&f,&t);
addedge(f,t,i);
}
dfs(1,0);
int ans=0xffffff,len=0;
for(int i=1;i<=n;i++){
if(maxson[i]<ans){
len=0;ans=maxson[i];
heap[len++]=i;
}
else if(maxson[i]==ans){
heap[len++]=i;
}
}
printf("%d %d\n",ans,len);
for(int i=0;i<len;i++){
printf("%d%c",heap[i],i==len-1?'\n':' ');
}
return 0;
}
快速切题 sgu134.Centroid 树形dp的更多相关文章
- SGU 134.Centroid( 树形dp )
一道入门树dp, 求一棵树的重心...我是有多无聊去写这种题...傻X题写了也没啥卵用以后还是少写好.. ----------------------------------------------- ...
- HDU 5977 Garden of Eden (树形dp+快速沃尔什变换FWT)
CGZ大佬提醒我,我要是再不更博客可就连一月一更的频率也没有了... emmm,正好做了一道有点意思的题,就拿出来充数吧=.= 题意 一棵树,有 $ n (n\leq50000) $ 个节点,每个点都 ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
- 树形DP
切题ing!!!!! HDU 2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...
- codeforces 709E E. Centroids(树形dp)
题目链接: E. Centroids time limit per test 4 seconds memory limit per test 512 megabytes input standard ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- 『战略游戏 最大利润 树形DP』
通过两道简单的例题,我们来重新认识树形DP. 战略游戏(luoguP1026) Description Bob喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的办法.现在他有个问题.他要 ...
- 树形dp 入门
今天学了树形dp,发现树形dp就是入门难一些,于是好心的我便立志要发一篇树形dp入门的博客了. 树形dp的概念什么的,相信大家都已经明白,这里就不再多说.直接上例题. 一.常规树形DP P1352 没 ...
- 树形DP ---- Codeforces Global Round 2 F. Niyaz and Small Degrees引发的一场血案
Aspirations:没有结果,没有成绩,acm是否有意义?它最大的意义就是让我培养快速理解和应用一个个未知知识点的能力. ————————————————————————————————————— ...
随机推荐
- Qt::浅谈信号槽连接,参数在多线程中的使用
Qt的信号槽有五种连接方式定义在enum Qt::ConnectionType,下面简单介绍 Qt::AutoConnection:自动判断连接方式,如果信号发送对象和执行槽对象在同一线程,那么等于Q ...
- 数据库SQL优化大总结之百万级数据库优化方案(转)
add by zhj: 作者没有指定是哪个数据库,这只是一个近似通用的总结.对于某个特定的数据库,有些条目可能并不适用. 原文:http://www.cnblogs.com/yunfeifei/p/3 ...
- conda
Conda是什么? Conda 是Anaconda下用于包管理和环境管理的命令行工具, Conda下一切都是包,包括Python和conda自己 Conda ≍ pip(包管理) + vitualen ...
- 《玩转Spring》第二章 BeanPostProcessor扩展
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shan9liang/article/details/34421141 上一章.介绍了怎样扩展spri ...
- centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课
centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 ...
- 【spring mvc】扒一扒tomcat
1.TOMCAT的目录 主要说一下webapps目录,WEB应用的目录结构:假设在$CATALINA_HOME/webapps下有helloapp的web应用 /helloapp:Web应用的根目录, ...
- rewrite or internal redirection cycle while processing "/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/jenkins/
折腾了很久,跟nginx配置没有关系.最终是把php版本从7.1降到5.6才解决的,是跟tp3.2匹配的
- 3.2 Templates -- The Application Template
1. 当你的应用程序启动时application模板是默认被渲染的的模板. 2. 你应该把你的header, footer和其他任何的装饰内容放到这里.此外,你应该有至少一个{{outlet}}:它是 ...
- 2017-2018 ACM-ICPC Asia East Continent League Final (ECL-Final 2017) Solution
A:Chat Group 题意:给出一个n, k 计算C(n, k) -> C(n,n) 的和 思路:k只有1e5 反过来想,用总的(2^ n) 减去 C(n, 0) -> C(n, k ...
- uva10537 dijkstra + 逆推
21:49:45 2015-03-09 传送 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8& ...