poj 3107 树重心
http://acm.hust.edu.cn/vjudge/problem/18069
和poj 1655差不多:http://www.cnblogs.com/qlky/p/5780933.html
区别就是要记录所有的重心
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 50000+5
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f #define ls (rt<<1)
#define rs (rt<<1|1) int n,m; int ptr = ,head[MAXN],vis[MAXN]; int res,ans[MAXN],son[MAXN],num=; struct node
{
int y,val,next;
}tree[MAXN<<]; void add(int fa,int son)
{
tree[ptr].y = son;
tree[ptr].next = head[fa];
head[fa] = ptr++;
} void dfs(int root)
{
vis[root] = ;
son[root] = ;
int tmp = ;
for(int i=head[root];i!=-;i=tree[i].next)
{
int y = tree[i].y;
if(vis[y]) continue;
dfs(y);
son[root] += son[y]+;
tmp = max(son[y]+,tmp);
}
tmp = max(tmp,n-son[root]-);
if(tmp<res)
{
ans[] = root;
num = ;
res = tmp;
}
else if(tmp == res) ans[num++] = root;
} int main()
{
int i,j,t,kase=;
while(~sf("%d",&n))
{
mem(tree,);
mem(head,-);
mem(vis,);
mem(ans,);
res=INF;
ptr = ;
num = ;
int x,y;
for(i=;i<n;i++)
{
sf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs();
sort(ans,ans+num);
pf("%d",ans[]);
for(i=;i<num;i++) pf(" %d",ans[i]);
blank;
}
return ;
}
poj 3107 树重心的更多相关文章
- POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)
树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...
- 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 1741 树的点分治(入门)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18205 Accepted: 5951 Description ...
- POJ 1655 Balancing Act && POJ 3107 Godfather
题目大意: 根据题目的图很好理解意思,就是记录每一个点的balance,例如 i 的balance就是把 i 从这棵树中除去后得到的森林中含有结点数最多 的子树中的节点个数,然后找到所有节点中对应的b ...
- cogs 1583. [POJ 3237] 树的维护 树链剖分套线段树
1583. [POJ 3237] 树的维护 ★★★★ 输入文件:maintaintree.in 输出文件:maintaintree.out 简单对比时间限制:5 s 内存限制:128 ...
- POJ 3107 Godfather (树重心)
题目链接:http://poj.org/problem?id=3107 题意: 数重心,并按从小到大输出. 思路: dfs #include <iostream> #include < ...
- 树形dp&&树的重心(D - Godfather POJ - 3107)
题目链接:https://cn.vjudge.net/contest/277955#problem/D 题目大意:求树的重心(树的重心指的是树上的某一个点,删掉之后形成的多棵树中节点数最大值最小). ...
随机推荐
- Python列表删除的三种方法
1.使用del语句删除元素 >>> i1 = ["a",'b','c','d'] >>> del i1[0] >>> prin ...
- JavaScript——原生js实现瀑布流
瀑布流介绍及实现原理: 瀑布流是一种页面布局,页面上也有多等宽的块(块就页面内容),每一块都是绝对定位(absolute),每个块排列的方式如下:寻找现在高度最小的列,把该块定位到该列下方.需要知道, ...
- P3554 [POI2013]LUK-Triumphal arch
\(\color{#0066ff}{ 题目描述 }\) 给一颗树,1号节点已经被染黑,其余是白的,两个人轮流操作,一开始B在1号节点,A选择k个点染黑,然后B走一步,如果B能走到A没染的节点则B胜,否 ...
- HiBench学习笔记
HiBench资源及教程:https://github.com/intel-hadoop/HiBench 介绍: Hadoop新人 环境 linux 该笔记针对英语弱鸡&自己在使用过程用遇到的 ...
- Linux系统centos中sudo命令不能用----提升权限
gyx is not in the sudoers file. This incident will be reported. 1.切换到root用户 su ,如果想要切换回去 exit 2.添加s ...
- freemarker常用标签解释
标签一: if else 你可以使用if,elseif和else指令来条件判断是否越过模板的一个部分.这些condition-s必须计算成布尔值,否则错误将会中止模板处理.elseif-s和else- ...
- C++使用using namespace std报错分析与解决方案
一句话概括,不能同时使用using 和include ***.h: 详细传送门:https://blog.csdn.net/m0_37876745/article/details/78565315
- CSS(十三).高度如何铺满全屏
该需求来源一次面试题. IE6不认识!important声明,IE7.IE8.Firefox.Chrome等浏览器认识:而在怪异模式中,IE6/7/8都不认识!important声明,这只是区别的一种 ...
- Django 10 GET和POST(HttpRequest对象,GET和POST请求,文件上传,HttpResponse对象的cookie)
Django 10 GET和POST(HttpRequest对象,GET和POST请求,文件上传,HttpResponse对象的cookie) 一.HttpRequest对象 #HttpRequest ...
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_new新建对象
CLR使用 new 操作符来创建新对象,例如:Employee e=new Employee("Param1"); 以下是 new 操作符所做的事情. 它计算类型及其所有基类型 ...