题目链接: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. python-time模块--pickle模块

    目录 time 模块 为什么要有time模块,time模块有什么用? time模块的三种格式 时间戳(timestamp) 格式化时间(需要自己定义格式) 结构化时间(struct-time) 结构化 ...

  2. LeetCode(134) Gas Station

    题目 There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...

  3. c#中利用“|”运算组合多项

    前几天看到一段代码 int i = GetCount(para1 | para2); 咋一看有些莫名奇妙,怎么传参的时候带了个或运算,其实这里面是有讲究的,查阅了各方资料,QQ群里赖着大牛问,才搞明白 ...

  4. python for data analysis chapter1~2

    Q1:numpy与series的区别:index Tab补全(任意路径Tab) 内省(函数:?显示文档字符串,??显示源代码:结合通配符:np.* load *?) %load .py ctrl-c( ...

  5. pycharm下搭建django开发环境

    在一次偶然的机会中,了解到万精油语言python,发现其流行程度发展迅速,于是也开始学习起来,正题. 1.安装python,查阅一些相关的资料及周边的开发工具,我选择python2(2.7.11),注 ...

  6. 03_HibernateSessionFactory源码分析

    文章导读: 讲解了一个线程为什么要使用同一个connection, 我们分析了HiberatenSessionFactory的实现机制, 然后根据Hibernate的写法重构了我们的代码. 最后测试可 ...

  7. Nginx从入门到放弃-第4章 深度学习篇

    4-1 Nginx动静分离_动静分离场景演示 4-2 Nginx动静分离_动静分离场景演示1 4-3 Nginx的动静分离_动静分离场景演示2 4-4 Rewrite规则_rewrite规则的作用 4 ...

  8. js时间格式化工具,时间戳格式化,字符串转时间戳

    在开发中经常会用到时间格式化,有时候在网上搜索一大堆但不是自己想要的,自己总结一下,写一个时间格式化工具方便以后直接使用,欢迎大家来吐槽…… 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  9. ACM-ICPC北京赛区2017网络同步赛

    E: Cats and Fish 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are many homeless cats in PKU campus. T ...

  10. C语言知识点(4)

    一.while.    dowhile. 1.while while (表达式) { 语句: … 语句: } 2.while do { printf(“%d/n,I);…}while (i<=1 ...