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(树的重心)的更多相关文章

  1. POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)

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

  2. poj 3107 Godfather(树的重心)

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7885   Accepted: 2786 Descrip ...

  3. Poj 2599 Godfather(树的重心)

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

  4. poj3107(树的重心,树形dp)

    题目链接:https://vjudge.net/problem/POJ-3107 题意:求树的可能的重心,升序输出. 思路:因为学树形dp之前学过点分治了,而点分治的前提是求树的重心,所以这题就简单水 ...

  5. poj3107 求树的重心(&& poj1655 同样求树的重心)

    题目链接:http://poj.org/problem?id=3107 求树的重心,所谓树的重心就是:在无根树转换为有根树的过程中,去掉根节点之后,剩下的树的最大结点最小,该点即为重心. 剩下的数的 ...

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

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

  7. POJ3107 (树的重心)

    ; INF=; type arr=record u,v,nt:longint; end; arr1=..maxn] of longint; ..maxn*] of arr; lt:..maxn] of ...

  8. [POJ3107] Godfather - 暴力枚举(树的重心)

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8728   Accepted: 3064 Descrip ...

  9. [poj3107]Godfather_树形dp_树的重心

    Godfather poj-3107 题目大意:求树的重心裸题. 注释:n<=50000. 想法:我们尝试用树形dp求树的重心,关于树的重心的定义在题目中给的很明确.关于这道题,我们邻接矩阵存不 ...

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

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

随机推荐

  1. FontDialog组件设置字体

    1.设置字体 private void button3_Click(object sender, EventArgs e) { this.fontDialog1.ShowDialog(); this. ...

  2. mysql UNIX时间戳与日期的相互转换 查询表信息

    UNIX时间戳转换为日期用函数FROM_UNIXTIME() select FROM_UNIXTIME(1156219870); 日期转换为UNIX时间戳用函数UNIX_TIMESTAMP() Sel ...

  3. linq query, using int.parse to convert varchar to int while orderby

    var t = from x in context.NewsLetterItem.ToList() //add .ToList at this place where x.APPId == appid ...

  4. KVC vs KVO(内容为转载记录,整合大家的总结为我所用)

    KVC即key-value coding的缩写, KVO即key-value observing的缩写 假如需要掌握Key-Value Observing机制,那么需要阅读本文应该有帮助.本文提供了K ...

  5. css3 图标上下移动动画

    @-webkit-keyframes bird{ 0% { -moz-transform: translate(0,0); -webkit-transform: translate(0,0); -o- ...

  6. jenkin系列_调度jmeter实现分布式测试

    假设现在有 192.168.1.100(jmeter 控制器 C ).192.168.1.101(jmeter负载机 B)两台机器进行分布式测试,各个步骤如下 1. C 和B 安装jmeter,并运行 ...

  7. UVA 11294 Wedding

    给n对夫妇安排座位,其中0h,0w分别表示新郎,新娘.同一对新郎,新娘不能坐在同一侧,而且互为通奸关系的人不能同时坐在新娘对面. 这道题目真是掉尽节操啊,,,欧美的氛围还是比较开放的. 分析: 首先说 ...

  8. C# Eval在asp.net中的用法及作用

    Eval( " ")和Bind( " ") 这两种一个单向绑定,一个双向绑定,bind是双向绑定,但需数据源支持 ASP.NET 2.0改善了模板中的数据绑定操 ...

  9. 基于ASP.NET的comet简单实现 http长连接,IAsyncResult

    http://www.cnblogs.com/hanxianlong/archive/2010/04/27/1722018.html 我潜水很多年,今天忽然出现.很久没写过博客了,不是因为不想写,而是 ...

  10. Android 滑动菜单SlidingMenu

    首先我们看下面视图: 这种效果大家都不陌生,网上好多都说是仿人人网的,估计人家牛逼出来的早吧,我也参考了一一些例子,实现起来有三种方法,我下面简单介绍下: 方法一:其实就是对GestureDetect ...