POJ 3107 Godfather (树的重心)
题意:求树的重心,若有多个,全部打印出来。
思路:
树的重心:在删除点v后,森林中的每棵树的节点数尽量均匀,若最大的那棵树的节点数最小,称v为树的重心。
这道题只是求树的所有重心,当且经当这棵树有对称性质时才有多重心,因此一棵树的重心最多不会超过2个。也是一遍DFS就可以搞定了,参考这个。
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <deque>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define INF 0x7f7f7f7f
#define LL long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
int n, cnt, edge_cnt, head[N]; struct node
{
int from, to, next;
node(){};
node(int from,int to,int next):from(from),to(to),next(next){};
}edge[N*]; void add_node(int from,int to)
{
edge[edge_cnt]=node(from,to,head[from]);
head[from]=edge_cnt++;
}
deque<int> que; int DFS(int t,int far)
{
node e;
int big=, sum=;
for(int i=head[t]; i!=-; i=e.next)
{
e=edge[i];
if(e.to==far) continue; int tmp=DFS(e.to, t);
big=max(big, tmp);
sum+=tmp;
}
big=max(big, n-sum-); if(big<=cnt)
{
if(big<cnt) que.clear();
cnt=big;
que.push_back(t);
}
return sum+;
} int main()
{
//freopen("input.txt", "r", stdin);
int a, b;
while(~scanf("%d",&n))
{
edge_cnt=;
cnt=INF;
memset(head,-,sizeof(head)); for(int i=; i<n; i++)
{
scanf("%d%d",&a,&b);
add_node(a,b);
add_node(b,a);
}
DFS(,-);
sort(que.begin(),que.end());
while(!que.empty())
{
printf("%d ",que.front());
que.pop_front();
}
puts("");
}
return ;
}
AC代码
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(树的重心)
嘟嘟嘟 题说的很明白,就是求树的重心. 我们首先dfs一遍维护每一个点的子树大小,然后再dfs一遍,对于一个点u,选择子树中size[v]最小的那个和n - size[u]比较,取最大作为删除u后的答 ...
随机推荐
- ElasticSearch基础之查询功能
[01]查询类型: [02]基本查询和组合查询是参与打分的 1.创建映射: 注意事项:基于上面映射的创建: "type": "keyword" # 如果某个字段 ...
- HDU - 3410 Passing the Message 单调递减栈
Passing the Message What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flo ...
- C#基础:线程之异步委托
线程:是程序中独立的指令流.在我们熟悉的Visual Studio编辑器中输入C# 代码的时候,系统会自动分析代码,提示你输入的代码出现的各种错误,这是一个后台线程完成的. 创建线程的一种简单的方式就 ...
- POJ3264 【RMQ基础题—ST-线段树】
ST算法Code: //#include<bits/stdc++.h> #include<cstdio> #include<math.h> #include< ...
- HDU2489【状压枚举】
题意: 给你n个点的图,然后让你在图里挑m个点,达到sumedge/sumnode最小 思路: 由于数据范围小,状压枚举符合m个点的状态,我是用vactor存了结点位置,也记录了结点的sum值,然后跑 ...
- lightoj1259 【素数预处理】
题意: 输出有多少对满足条件的(a,b) both a and b are prime; a+b=n a<=b; 思路: 一开始想的就是打表一个素数数组,然后还去二分..mdzz..直接判断一下 ...
- Unity json
MiniJSON.cs using UnityEngine; using System; using System.Collections; using System.Collections.Gene ...
- [Xcode 实际操作]七、文件与数据-(22)使用OCR光学字符识别技术识别银行卡号码
目录:[Swift]Xcode实际操作 本文将演示如何使用光学字符识别技术,识别信用卡上的卡号. OCR技术是光学字符识别的缩写(Optical Character Recognition), 是通过 ...
- css 所有选择器 实例与总结
目录 什么是选择器? 选择器都有那些呢? 标签选择器 ID选择器 类选择器 后代选择器 子代选择器 组合选择器 交集选择器 相邻兄弟选择器 通用兄弟选择器 属性选择器 伪类选择器 什么是选择器? 在c ...
- Rsync备份同步数据工具
Rsync is a fast and extraordinarily versatile file copying tool. Rsync是一款开源的,快速的,多功能的,可实现全量和增量的本地 ...