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的更多相关文章

  1. SGU 134.Centroid( 树形dp )

    一道入门树dp, 求一棵树的重心...我是有多无聊去写这种题...傻X题写了也没啥卵用以后还是少写好.. ----------------------------------------------- ...

  2. HDU 5977 Garden of Eden (树形dp+快速沃尔什变换FWT)

    CGZ大佬提醒我,我要是再不更博客可就连一月一更的频率也没有了... emmm,正好做了一道有点意思的题,就拿出来充数吧=.= 题意 一棵树,有 $ n (n\leq50000) $ 个节点,每个点都 ...

  3. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

  4. 树形DP

    切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...

  5. codeforces 709E E. Centroids(树形dp)

    题目链接: E. Centroids time limit per test 4 seconds memory limit per test 512 megabytes input standard ...

  6. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  7. 『战略游戏 最大利润 树形DP』

    通过两道简单的例题,我们来重新认识树形DP. 战略游戏(luoguP1026) Description Bob喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的办法.现在他有个问题.他要 ...

  8. 树形dp 入门

    今天学了树形dp,发现树形dp就是入门难一些,于是好心的我便立志要发一篇树形dp入门的博客了. 树形dp的概念什么的,相信大家都已经明白,这里就不再多说.直接上例题. 一.常规树形DP P1352 没 ...

  9. 树形DP ---- Codeforces Global Round 2 F. Niyaz and Small Degrees引发的一场血案

    Aspirations:没有结果,没有成绩,acm是否有意义?它最大的意义就是让我培养快速理解和应用一个个未知知识点的能力. ————————————————————————————————————— ...

随机推荐

  1. ubuntu常用操作命令以及它的通道模式简解

    1.tail tail -f filename :可以动态查看文件的写入,按ctrl+c结束查看. 要显示 notes 文件的最后十行,输入: tail -n -10 notes tail notes ...

  2. 产品开发过程描述xmind

  3. stm8s 时钟库函数选择内部RC初始化

    //本文选择16M内部RC震荡.分频为1 即系统时钟为16M void CLK_HSICmd(FunctionalState NewState) { /* Check the parameters * ...

  4. 203-ReactDOM

    一.概述 加载方式: <script> ES6:import ReactDOM from 'react-dom' ES5:var ReactDOM = require('react-dom ...

  5. java-基础-【四】实际编程中的对象

    一.概述 实际编程开发中,仅仅一个数据库对象映射是满足不了各种复杂需求. O/R Mapping 是 Object Relational Mapping(对象关系映射)的缩写.通俗点讲,就是将对象与关 ...

  6. WMS学习笔记:2.WMS解析

    WMS 定义了三个操作,分别是:GetCapabilities,GetMap和GetFeatureInfo.其中,GetFeatureInfo是可选的.本条款规定了在超文本传输协议(HTTP)分布式计 ...

  7. phpstorm psr2样式.xml

    将如下内容保存为 .xml 格式 <code_scheme name="Default"> <PHPCodeStyleSettings> <optio ...

  8. HDFS中的读写数据流

    1.文件的读取 在客户端执行读取操作时,客户端和HDFS交互过程以及NameNode和各DataNode之间的数据流是怎样的?下面将围绕图1进行具体讲解. 图 1 客户端从HDFS中读取数据 1)客户 ...

  9. PAT 1117 Eddington Number [难]

    1117 Eddington Number (25 分) British astronomer Eddington liked to ride a bike. It is said that in o ...

  10. 20165324_mybash

    20165324_mybash 实验要求 实验要求: 使用fork,exec,wait实现mybash 写出伪代码,产品代码和测试代码 发表知识理解,实现过程和问题解决的博客(包含代码托管链接) 背景 ...