嘟嘟嘟

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

我们首先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. [javaSE] 数据结构(栈)

    栈(stack)是一种线性存储结构,有以下特点: 1.栈中数据是按照先进后出的方式进出栈的 2.向栈中添加删除元素时,只能从栈顶进行操作 使用数组实现栈 定义一个类ArrayStack 实现入栈方法p ...

  2. 鸟哥linux私房菜学习笔记 第二章知识点

    2.1 linux一切皆文件 2.2 磁盘分区 磁盘即文件 2.2.1 磁盘连接的方式与设备文件名的关系 模糊 1.正常的实体机器大概使用的都是 /dev/sd[a-] 的磁盘文件名,至于虚拟机环境下 ...

  3. JPA 实体映射

    一.实体基本映射 /* * @Entity:将领域对象标注为一个实体,表示保存到数据库中 * @@Table:保存到数据库中表名,默认表名为类名,可通过name属性命名 * * */ @Entity ...

  4. 将Windows上的Oracle迁移至Linux

    迁移前提: 1.在安装Linux数据库实例时,注意选择的编码格式要与Windows的数据库实例一致. 迁移步骤 1.检查Linux上数据库实例的编译格式 SQL> select userenv( ...

  5. SecureCRT介绍

    SecureCRT® combines rock-solid terminal(安全兼备可靠的终端) emulation with the strong encryption, broad range ...

  6. js实现队列结构

    创建队列 let items function Queue { this.enqueue = function(element){ items.push(element) } this.dequeue ...

  7. 在 :after/ :before 使用 font awesome web Icon

    .element { position: relative; } /*replace the content value with the corresponding value from the l ...

  8. BZOJ3512:DZY Loves Math IV

    传送门 Sol 好神仙的题目.. 一开始就直接莫比乌斯反演然后就 \(GG\) 了 orz 题解 permui 枚举 \(n\),就是求 \(\sum_{i=1}^{n}S(i,m)\) 其中\(S( ...

  9. JavaScript函数与面向对象

    一.JS面向对象 function Func(name,age){ //this = obj this.Name = name; this.Age = age; } obj = new Func('r ...

  10. bootstrap学习笔记细化(标题)

    bootstrap中的排版: 标题(h1~h6/.h1~.h6) h1:36px;h2:30px;h3:24px;h4:18px;h5:14px;h6:12px; 副标题(small) 小练习(标题大 ...