题目链接:http://poj.org/problem?id=3107

题意:

数重心,并按从小到大输出。

思路:

dfs

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 5e4 + ;
struct Edge {
int next, to;
}edge[N << ];
vector <int> G[N];
int dp[N], n, head[N], cnt; inline void add(int u, int v) {
edge[cnt].next = head[u];
edge[cnt].to = v;
head[u] = cnt++;
} int dfs(int u, int p) {
dp[u] = ;
int sum = ;
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == p)
continue;
int temp = dfs(v, u);
dp[u] = max(dp[u], temp);
sum += temp;
}
dp[u] = max(dp[u], n - sum - );
return sum + ;
} int main()
{
while(~scanf("%d", &n)) {
for(int i = ; i <= n; ++i) {
head[i] = -;
}
cnt = ;
for(int i = ; i < n; ++i) {
int u, v;
scanf("%d %d", &u, &v);
add(u, v);
add(v, u);
}
dfs(, -);
int Max = n;
vector <int> ans;
for(int i = ; i <= n; ++i) {
Max = min(Max, dp[i]);
}
for(int i = ; i <= n; ++i) {
if(Max == dp[i]) {
ans.push_back(i);
}
}
for(int i = ; i < ans.size(); ++i) {
if(i == ans.size() - ) {
printf("%d\n", ans[i]);
} else {
printf("%d ", ans[i]);
}
}
}
return ;
}

POJ 3107 Godfather (树重心)的更多相关文章

  1. POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

  2. poj 3107 Godfather(树的重心)

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7885   Accepted: 2786 Descrip ...

  3. POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)

    关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...

  4. # [Poj 3107] Godfather 链式前向星+树的重心

    [Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...

  5. poj 3107 Godfather 求树的重心【树形dp】

    poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...

  6. POJ 1655 Balancing Act && POJ 3107 Godfather

    题目大意: 根据题目的图很好理解意思,就是记录每一个点的balance,例如 i 的balance就是把 i 从这棵树中除去后得到的森林中含有结点数最多 的子树中的节点个数,然后找到所有节点中对应的b ...

  7. POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)

    参考网址:http://blog.csdn.net/acdreamers/article/details/16905653   树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...

  8. POJ 3107 Godfather(树的重心)

    嘟嘟嘟 题说的很明白,就是求树的重心. 我们首先dfs一遍维护每一个点的子树大小,然后再dfs一遍,对于一个点u,选择子树中size[v]最小的那个和n - size[u]比较,取最大作为删除u后的答 ...

  9. POJ 3107 Godfather (树的重心)

    题意:求树的重心,若有多个,全部打印出来. 思路: 树的重心:在删除点v后,森林中的每棵树的节点数尽量均匀,若最大的那棵树的节点数最小,称v为树的重心. 这道题只是求树的所有重心,当且经当这棵树有对称 ...

随机推荐

  1. Lecture 3

    surface models 1. The two main methods of creating surface models are interpolation and triangulatio ...

  2. ubuntu gcc的下载链接,比较快的。

    http://mirrors.163.com/ubuntu-releases/ http://xhmikosr.1f0.de/tools/msys/

  3. NSNotificationCenter的用法

    作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的. 注册通知:即要在什么地方接受消息 [[NSNotificationCenter defaultCenter]  ...

  4. noip2017行记

    前奏 写了所有的变量名在linux下测,结果发现并没什么用...听说将所有的变量加上下划线,加上自己的名字作为前缀.. lgj,“感觉今年有网络流,今年要立个flag”,zjr“你咋不上天儿” 在车上 ...

  5. 装饰器与lambda

    装饰器 实际上理解装饰器的作用很简单,在看core python相关章节的时候大概就是这种感觉.只是在实际应用的时候,发现自己很难靠直觉决定如何使用装饰器,特别是带参数的装饰器,于是摊开来思考了一番, ...

  6. 《小团团团队》第九次团队作业:Beta冲刺与验收准备

    项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 实验十三 团队作业9:Beta冲刺与团队项目验收 团队名称 小团团团队 作业学习目标 (1)掌握软件黑盒测试技术:(2)学 ...

  7. bootstrap 弹出框(Popover)插件 修改title等属性选项值

    <button type="button" class="btn btn-default ht-btn" data-toggle="popove ...

  8. day06 面向对象编程

    面向对象: 面向对象: 世界万物,皆可分类 世界万物,皆为对象   只要是对象,就肯定属于某种品类 只要是对象,就肯定有属性         特性: 多态: 一个统一的接口,多种实现  例如:  一个 ...

  9. RF操作滚动条(竖拉)

    方式一:window.scrollBy(0, document.body.scrollHeight) 方式二:window.scrollTo(0, document.body.scrollHeight ...

  10. javascript学习笔记 - 变量、作用域和内存问题

    一 垃圾收集 javascript具有自动垃圾收集机制.由垃圾收集机制标找出不再使用的变量.按照固定间隔的时间进行销毁,释放内存.  1.找出不再使用的变量的方法,如下: 1-1.标记清除 垃圾回收器 ...