POJ 3107 Godfather(树的重心)
题说的很明白,就是求树的重心。
我们首先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(树的重心)的更多相关文章
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
- poj 3107 Godfather(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7885 Accepted: 2786 Descrip ...
- POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)
参考网址:http://blog.csdn.net/acdreamers/article/details/16905653 树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...
- Poj 2599 Godfather(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Description Last years Chicago was full of gangste ...
- POJ 3107 Godfather (树重心)
题目链接:http://poj.org/problem?id=3107 题意: 数重心,并按从小到大输出. 思路: dfs #include <iostream> #include < ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
- # [Poj 3107] Godfather 链式前向星+树的重心
[Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...
- poj 3107 Godfather 求树的重心【树形dp】
poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...
- POJ 3107 Godfather (树的重心)
题意:求树的重心,若有多个,全部打印出来. 思路: 树的重心:在删除点v后,森林中的每棵树的节点数尽量均匀,若最大的那棵树的节点数最小,称v为树的重心. 这道题只是求树的所有重心,当且经当这棵树有对称 ...
随机推荐
- 4 springboot 集成swagger2
Swagger:实时生成在线接口文档,方便测试和沟通 官网地址:https://swagger.io/ 引入依赖 <dependency> <groupId>io.spring ...
- 判定 java 对象死亡的过程
- poj 2796 Feel Good 单调队列
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8753 Accepted: 2367 Case Ti ...
- RESTORE DATABASE命令还原SQLServer 2005 数据库
--返回由备份集内包含的数据库和日志文件列表组成的结果集. --主要获得逻辑文件名 USE master RESTORE FILELISTONLY FROM DISK = 'g:\back.Bak' ...
- 利用webstorm 快捷创建标签案例:
利用webstorm 快捷创建标签案例: .wrapper>(.sWrapper>input[class='sText']+span[class= 'btn']*3)+.flWrapper ...
- bzoj一句话题解
发现好多人都在搞这个...本人也想来试试(Solved刚到70就搞这个靠不靠谱啊喂).会更新的.嗯. 1000-1029 1000 A+B problem (这个还需要一句话吗?). 1001 狼抓兔 ...
- Sass、Less、Stylus,我选Sass!
Sass官网 | Sass中文 简介:待添加 Less 简介:待添加 Stylus 简介:待添加
- 【es6】Generator 函数
1. 基本概念 状态机,封装了多个内部状态 2. 应用 返回一个遍历器对象. 3. 代码形式 function* helloWorldGenertor() { yield 'hello'; yield ...
- input标签添加上disable属性在ios端字体颜色不兼容的问题
input[disabled],input:disabled,input.disabled{ color: #3e3e3e; -webkit-text-fill-color: #3e3e3e; -we ...
- 常用的自动化测试框架及测试框架的发展(Alpha)
前言:自动化测试在过去的20年已经有了很大的发展.最初的测试工具只提供了简单的捕捉/回访功能,维护性较差.而且脚本工具实现需要很强的开发技术和经验,而且数量众多的测试脚本加上没有文档记录因此维护起来较 ...