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 ...
随机推荐
- 【git】切换分支获取代码
Welcome to Git (version 1.9.5-preview20150319) Run 'git help git' to display the help index.Run 'git ...
- MVC5 学习笔记2
去除VS Browser Link废代码 在webconfig中添加 <configuration> <appSettings> <add key="vs:En ...
- poj 3373 Changing Digits (DFS + 记忆化剪枝+鸽巢原理思想)
http://poj.org/problem?id=3373 Changing Digits Time Limit: 3000MS Memory Limit: 65536K Total Submi ...
- 学无止境,学习AJAX(一)
什么是AJAX?异步JavaScript和XML. AJAX是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的 ...
- iOS 页面间传值 之 单例传值 , block 传值
ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...
- writeToFile 读写文件问题
关于 writeToFile 读写文件:当字典中键值对以 Model(例如:studentModel)为值时发现 Dictionary 调用 writeToFile 方法无法生成 plist 文件,经 ...
- css 多行显示省略号....
CSS属性如下: white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
- CSS 实现背景半透明
IE过渡滤镜 + CSS3 rgba 即可完美实现. 具体实现代码如下: .transparent { background:rgba(0, 0, 0, 0.3); filter:pr ...
- HDU 2986 Ballot evaluation(精度问题)
点我看题目 题意 : 给你n个人名,每个名后边跟着一个数,然后m个式子,判断是否正确. 思路 :算是一个模拟吧,但是要注意浮点数容易丢失精度,所以要好好处理精度,不知道多少人死在精度上,不过我实在是不 ...
- [Gauss]HDOJ3976 Electric resistance
题意: 一看图就明白了 要求的是1与n端点间的等效电阻 重点在于转化成考虑电流 根据KCL定理:在任一瞬间流出(流入)该节点的所有电流的代数和恒为零 U = IR 可以令1点的电势为零 那么n点的电势 ...