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

----------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 16009;
int sz[maxn], N;
int ans[maxn], n, ansV;
struct edge {
int to;
edge* next;
} E[maxn << 1], *pt = E, *head[maxn];
void AddEdge(int u, int v) {
pt->to = v; pt->next = head[u]; head[u] = pt++;
}
void Init() {
scanf("%d", &N);
for(int i = 1; i < N; i++) {
int u, v; scanf("%d%d", &u, &v); u--; v--;
AddEdge(u, v);
AddEdge(v, u);
}
n = 0;
ansV = maxn;
}
void dfs(int x, int fa = -1) {
sz[x] = 1;
int mx = 0;
for(edge* e = head[x]; e; e = e->next) if(e->to != fa) {
dfs(e->to, x);
mx = max(mx, sz[e->to]);
sz[x] += sz[e->to];
}
mx = max(mx, N - sz[x]);
if(mx < ansV)
ansV = mx, ans[0] = x, n = 1;
else if(mx == ansV)
ans[n++] = x;
}
int main() {
Init();
dfs(0);
printf("%d %d\n", ansV, n);
sort(ans, ans + n);
for(int i = 0; i < n; i++)
printf("%d ", ++ans[i]);
return 0;
}

----------------------------------------------------------------

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

Author : Mugurel Ionut Andreica
Resource : SSU::Online Contester Fall Contest #2
Date : Fall 2002

pasting

转载于:https://www.cnblogs.com/JSZX11556/p/5046556.html

SGU 134.Centroid( 树形dp )相关推荐

  1. BNUOJ 52305 Around the World 树形dp

    题目链接: https://www.bnuoj.com/v3/problem_show.php?pid=52305 Around the World Time Limit: 20000msMemory ...

  2. [树形dp] Jzoj P5233 概率博弈

    Description 小A和小B在玩游戏.这个游戏是这样的: 有一棵n个点的以1为根的有根树,叶子有权值.假设有m个叶子,那么树上每个叶子的权值序列就是一个1->m 的排列. 一开始在1号点有 ...

  3. fwt优化+树形DP HDU 5909

    1 //fwt优化+树形DP HDU 5909 2 //见官方题解 3 // BestCoder Round #88 http://bestcoder.hdu.edu.cn/ 4 5 #include ...

  4. BZOJ 1040 ZJOI2008 骑士 树形DP

    题目大意:给定一个基环树林,每一个点上有权值,要求选择一个权值和最大的点集,要求点集中的随意两个点之间不能直接相连 最大点独立集--考虑到n<=100W,网络流铁定跑不了,于是我们考虑树形DP ...

  5. POJ 3342 树形DP+Hash

    这是很久很久以前做的一道题,可惜当时WA了一页以后放弃了. 今天我又重新捡了起来.(哈哈1A了) 题意: 没有上司的舞会+判重 思路: hash一下+树形DP 题目中给的人名hash到数字,再进行运算 ...

  6. [NC15748]旅游 树形dp基础

    菜鸡第一次接触树形dp这个东西,不过这个东西还是很好理解的(可能是因为模板题吧) 个人感觉,相比线性dp,树形dp的状态转移方程更加的直观,难点主要是在"树"的结构上比较麻烦. 题 ...

  7. 容斥 + 树形dp ---- 2021 icpc 沈阳 L Perfect Matchings

    题目链接 题目大意: 就是给你一个2n2n2n个点的完全图,从这个图里面抽出2n−12n-12n−1条边,这些边形成一颗树,现在问你剩下的图里面点进行完美匹配有多少种方案? 解题思路: 一开始被完美匹 ...

  8. 树形dp ---- gym101667 A(贪心 + 树形dp + 两个dp方程组维护)

    题目链接 题目大意: 就是一棵5e35e35e3的树,可以选择一些点,放上基站,如果uuu上的基站价值为ddd,那么距离uuu小于等于ddd的点都会被覆盖,问使得整棵树被覆盖需要的最小价值. 解题思路 ...

  9. 树形dp ---- 2018年杭电多校第二场 H travel

    题目大意: 就是给你一个带点权的树,找到3条独立互不相交的路径使得权值和最大 解题思路: 很经典的树形dp 我们设dp[root][j][k]dp[root][j][k]dp[root][j][k]表 ...

最新文章

  1. 科普:5G网络关键技术详解
  2. DOMINO的JDBC和ODBC连接方法
  3. lunix 命令积累
  4. oracle 修改聚簇因子,Oracle 基础篇 --- 聚簇因子(clustering_factor)
  5. PHP中file_get_contents的使用方法
  6. Java连接Mysql数据库增删改查实现
  7. docker快速入门_Docker标签快速入门
  8. deepin linux grub,deepin linux 安装之后 引导错误 出现 grub
  9. STS安装 activiti-designer-5.18.0插件
  10. Linux RabbitMQ+Erlang /sbin/service rabbitmq-server start 启动报错
  11. POJ1011 HDU1455 UVA307 UVALive5522 Sticks题解
  12. java中jsp table标签属性_JSP自定义标签-属性
  13. CCNA 笔记-11
  14. COMSOL Multiphysics 多物理场仿真学习小记
  15. java地铁线路规划_北京地铁线路规划
  16. c语言编译器中cu是什么意思,.c文件调用.cu中的函数
  17. C语言小熊时钟实验报告,用VC6.0编写一个小熊时钟(求代码)
  18. Android的main方法,Android程序没有main函数,那Android程序的执行入口在哪儿?
  19. android共享win10,如何与Android手机共享Win10笔记本电脑网络
  20. DSP篇--C6701功能调试系列之 FLASH测试

热门文章

  1. Java自学手记——多态
  2. Extjs创建多个application实现多模块MVC动态加载。。
  3. (转)基本光照模型公式
  4. Nginx之gzip压缩配置
  5. python 3.x 不再支持MySQLdb 模块
  6. (201)数字6种表示方式
  7. (80)FPGA复位激励(function)
  8. ffmpeg h265编码_基于ffmpeg库mp4编码记录。
  9. apb外设怎么分配地址
  10. 数值计算方法(零)——运算的要求+基本算法