POJ3107--Godfather(树的重心)
vector建图被卡了。。改为链式前向星500ms过的。。差了四倍多?。。。
表示不太会用链表建图啊。。自己试着写的,没看模板。。嗯。。果然错了。。落了一句话orz
树的重心就是找到一个树中一个点,其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心,删去重心后,生成的多棵树尽可能平衡。
根据定义很容易求得。
/***********************************************
Problem: 3107 User: G_lory
Memory: 4440K Time: 500MS
Language: C++ Result: Accepted
***********************************************/ #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;
typedef long long ll; const int N = 500005; struct Edge {
int to, next;
} edge[N];
int head[N]; int cnt;
int sz[N];
int maxn[N]; int n; void add_edge(int u, int v) // 双向边
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt;
cnt++; edge[cnt].to = u;
edge[cnt].next = head[v];
head[v] = cnt;
cnt++;
} int ans;
void dfs(int u, int pre)
{
sz[u] = 0;
int temp = 0;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (v == pre) continue;
dfs(v, u);
sz[u] += sz[v] + 1;
temp = max(temp, sz[v] + 1);
}
temp = max(n - sz[u] - 1, temp);
maxn[u] = temp;
ans = min(temp, ans);
} int main()
{
while (~scanf("%d", &n))
{
int a, b;
memset(head, -1, sizeof head);
cnt = 1;
for (int i = 1; i < n; ++i)
{
scanf("%d%d", &a, &b);
add_edge(a, b);
}
ans = n;
dfs(1, -1);
int ok = 1;
for (int i = 1; i <= n; ++i)
{
if (maxn[i] == ans)
{
if (ok) ok = 0;
else printf(" ");
printf("%d", i);
}
}
printf("\n");
}
return 0;
}
POJ3107--Godfather(树的重心)的更多相关文章
- POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)
参考网址:http://blog.csdn.net/acdreamers/article/details/16905653 树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...
- poj 3107 Godfather(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7885 Accepted: 2786 Descrip ...
- Poj 2599 Godfather(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Description Last years Chicago was full of gangste ...
- poj3107(树的重心,树形dp)
题目链接:https://vjudge.net/problem/POJ-3107 题意:求树的可能的重心,升序输出. 思路:因为学树形dp之前学过点分治了,而点分治的前提是求树的重心,所以这题就简单水 ...
- poj3107 求树的重心(&& poj1655 同样求树的重心)
题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
- POJ3107 (树的重心)
; INF=; type arr=record u,v,nt:longint; end; arr1=..maxn] of longint; ..maxn*] of arr; lt:..maxn] of ...
- [POJ3107] Godfather - 暴力枚举(树的重心)
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8728 Accepted: 3064 Descrip ...
- [poj3107]Godfather_树形dp_树的重心
Godfather poj-3107 题目大意:求树的重心裸题. 注释:n<=50000. 想法:我们尝试用树形dp求树的重心,关于树的重心的定义在题目中给的很明确.关于这道题,我们邻接矩阵存不 ...
- POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)
关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...
随机推荐
- UIAlertView带textField
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"校验登录密码" message:@"" del ...
- c++ 类的对象与指针
这里首先我们需区分一下指针数组和数组指针. 指针数组:int *p[4];它最终是个数组,只是这个数组存储的是4个指向int类型的指针. 数组指针:int (*P)[4];它最终是个指针,表示一个指向 ...
- C++ new operator, delete operator, operator new, operator delete, new placement
http://www.younfor.com/cpp-new-placement-new-operator-new.html http://www.cnblogs.com/luxiaoxun/arch ...
- Ubutn14.04下caffeine工具不显示在工具栏中的问题
安装过程请参考Ubuntu 14.04下安装Caffeine 2.6.2 阻止显示器进入睡眠状态 至于为什么不显示在任务栏,这不是程序的bug,你可以平ps -e看一下,任务已经在运行. 其实这是新版 ...
- TSS 任务状态段
TSS(任务状态段) 1 什么是TSS TSS 全称task state segment,是指在操作系统进程管理的过程中,任务(进程)切换时的任务现场信息. 2 TSS工作细节 TSS在任务切换过程 ...
- github Git 原理简介
由于Git是一个DVCS(Distributed Version Control System,分布式版本控制系统),不同于传统的CVS/SVN版本系统那样必须由一个中央服务器来管理所有的版本记录,它 ...
- 怎样在WINDOWS下面编译LIBCURL
我测试过,好像没OK This is a short note about building cURL with SSL support on Windows. Tools required: cUR ...
- 创建安卓app的30个经验教训
在添加任何第三方party之前,请三思:这真的是一个成熟的项目吗? 如果一个东西用户看不到,就不要绘制它! 除非真的需要,否则别使用数据库: 达到65k方法数限制来的非常快,真的,非常快!不过 mul ...
- C 语言函数指针
c代码: #include <stdio.h> int add(int x,int y); int subtract(int x,int y); int domath(int (*math ...
- hdu4432 Sum of divisors(数论)
Sum of divisors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...