嘟嘟嘟

题说的很明白,就是求树的重心。

我们首先dfs一遍维护每一个点的子树大小,然后再dfs一遍,对于一个点u,选择子树中size[v]最小的那个和n - size[u]比较,取最大作为删除u后的答案Max[u]。

然后再O(n)遍历一遍取min(Max[i]).

写代码的时候两次dfs可以合并。

然后这题竟然卡vector,不得不用链前存图……简直有毒。

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 5e4 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n;
struct Node
{
int nxt, to;
}e[maxn << ];
int head[maxn], ecnt = ;
void add(int x, int y)
{
e[++ecnt].to = y;
e[ecnt].nxt = head[x];
head[x] = ecnt;
} bool vis[maxn];
int siz[maxn], Max[maxn];
void dfs(int now, int fa)
{
siz[now] = ; Max[now] = -;
for(int i = head[now]; i; i = e[i].nxt)
{
if(e[i].to == fa) continue;
dfs(e[i].to, now);
siz[now] += siz[e[i].to];
Max[now] = max(Max[now], siz[e[i].to]);
}
Max[now] = max(Max[now], n - siz[now]);
} int main()
{
n = read();
Mem(vis, ); Mem(head, ); ecnt = ;
for(int i = ; i < n; ++i)
{
int x = read(), y = read();
add(x, y); add(y, x);
}
dfs(, );
int Min = INF;
for(int i = ; i <= n; ++i) Min = min(Min, Max[i]);
for(int i = ; i <= n; ++i) if(Max[i] == Min) write(i), space;
enter;
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 BalanceAct 3107 Godfather (树的重心)(树形DP)

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

  4. Poj 2599 Godfather(树的重心)

    Godfather Time Limit: 2000MS Memory Limit: 65536K Description Last years Chicago was full of gangste ...

  5. POJ 3107 Godfather (树重心)

    题目链接:http://poj.org/problem?id=3107 题意: 数重心,并按从小到大输出. 思路: dfs #include <iostream> #include < ...

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

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

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

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

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

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

  9. POJ 3107 Godfather (树的重心)

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

随机推荐

  1. 集群搭建之Hive配置要点

    注意点: 在启动Hive 的时候要先启动Hadoop和MySQL服务. Mysql 和 Hive 搭建在 yan00机器上. part1:MySQL配置相关 安装和配置相关命令: Yum instal ...

  2. 撩课-Java每天5道面试题第14天

    101.请解释下 ORM? 对象关系映射(Object Relational Mapping)模式 是一种为了解决面向对象与关系数据库 存在的互不匹配的现象的技术. 简单来说, ORM是通过使用描述对 ...

  3. java核心技术-IO

    一 .IO 1.1 流的简单介绍和分类 Java流操作的相关的类和接口: Java流类图结构: 四个抽象基类分别为:InputStream .OutputStream .Reader .Writer: ...

  4. 【SSH网上商城项目实战16】Hibernate的二级缓存处理首页的热门显示

    转自:https://blog.csdn.net/eson_15/article/details/51405911 网上商城首页都有热门商品,那么这些商品的点击率是很高的,当用户点击某个热门商品后需要 ...

  5. python学习之老男孩python全栈第九期_day017知识点总结——初识递归、算法

    一. 递归函数 如果一个函数在内部调用自身本身,这个函数就是递归函数. 最大递归深度默认是997 -- python从内存角度出发做得限制(而不是程序真的报错),最大深度可以修改 def func(n ...

  6. Algorithm——整数反转

    一.问题 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 12 ...

  7. [COCI2006-2007#1] Bond

    状压DP \(dp[i]\)表示当前选人状态为\(i\)且选择了前\(i.count()\)个物品时最大的概率 #include"cstdio" #include"cst ...

  8. css 各种常见布局整理

    在学习各种布局之前我们先来认识各个关键词,理解这些关键词,然后由点到面,这样就简单多了. display属性 页面中每个元素都有一个默认的display属性,它的值与该元素的类型有关,默认值通常是 b ...

  9. MUI框架-12-使用原生底部选项卡(凸出图标案例)

    MUI框架-12-使用原生底部选项卡(凸出图标案例) 今天,用 mui 做 app 时,遇到了可能各位都遇到过的头疼问题:底部中间图标凸起,如下图: 最后有源代码 [提示]:有人问我在 HBuilde ...

  10. centos7 安装 maven 和ant git 以及 rocketmq 4.2安装过程(安装成功,调用失败)

    1.maven 安装 wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /et ...