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. 使用git和gitlab进行协同开发流程

    一.基本概念 1.仓库(Repository) ①源仓库(线上版本库) 在项目的开始,项目的发起者构建起一个项目的最原始的仓库,称为origin. 源仓库的有两个作用: 1.汇总参与该项目的各个开发者 ...

  2. CF909F AND-permutations 构造

    正解:构造 解题报告: 传送门! QAQ我jio得还挺难的,,,构造+数论什么的果然还是不适合灵巧这种菜菜啊QAQ 不过理解了的话也就没有那么难?所以还是港下QAQ 首先看task1 首先要发现一个, ...

  3. Celery和Rabbitmq自学

    异步消息队列,也能用于定时和周期性任务.每次修改的task代码还要重启worker,这个有点麻烦 所有带task()装饰器的可调用对象(usertask)都是celery.app.task.Task类 ...

  4. 第1章 1.8计算机网络概述--OSI参考模型和网络排错

    OSI参考模型的网络排错: 每一层都为上一层提供服务. 如果网络出故障了,应该从底层向高层一层一层的查. OSI参考模型排错指导:(排错原则:自下而上.终极大招ping命令) 1.物理层故障: ①查看 ...

  5. H5保养

    合成油上面写着A3B4级,可以用的.绿静发动机说明书上写着呢CI-4级或A3B4级都可以用. 你的车换机油要加多少升? 5.6标准数,先加5L后看机油尺,在中间就行, 多点少点只要不过上线就无所谓. ...

  6. mysql 数据操作 多表查询 子查询 介绍

    子查询就是: 把一条sql语句放在一个括号里,当做另外一条sql语句查询条件使用 拿到这个结果以后 当做下一个sql语句查询条件mysql 数据操作  子查询 #1:子查询是将一个查询语句嵌套在另一个 ...

  7. xutil3 post 和 get请求

    https://i.cnblogs.com/EditPosts.aspx?postid=7001253 compile 'org.xutils:xutils:3.3.36' 注册xutil3 < ...

  8. 离线状态 Postman不能开启Postman Interceptor解决

    目前的postman插件如果想正常使用,必须安装Postman Interceptor插件,这样才能直接使用chrome浏览器的cookie等信息,否则postman是无法完成老版本的功能的. 直接使 ...

  9. Hyperledger Fabric 开发环境搭建 centos7系统

    一.安装GO语言 下载最新版的go 打开Terminal,输入命令(以下命令都是以root管理员的角色进行的) su 输入密码:***** wget https://storage.googleapi ...

  10. Cocos2d-JS实现的打飞机

    一.前言 今天我们来讲一个最最最常见的一个小游戏--打飞机!是的,打飞机!还记得小时候在玩儿的雷电,应该是打飞机最早的样子了吧.直到现在,也有微信打飞机,全民飞机大战,全民打飞机等游戏的出现,这些游戏 ...