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 题目大意:求树的重心(树的重心指的是树上的某一个点,删掉之后形成的多棵树中节点数最大值最小). ...
随机推荐
- git rm 与 git reset
https://www.cnblogs.com/sunshine-xin/articles/3521481.html 1. git rm --cached file will remove the f ...
- AngularJS(二)——常见指令以及下拉框实现
前言 学完AngularJS,总体上感觉没什么新鲜的东西,但是又感觉每一步都很新鲜,因为没有见过,又因为学到的语法函数和JavaScript差不多,本篇主要介绍一些AngularJS的指令,常见指令和 ...
- poj 1743 Musical Theme(最长重复子串 后缀数组)
poj 1743 Musical Theme(最长重复子串 后缀数组) 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复 ...
- jzoj4915. 【GDOI2017模拟12.9】最长不下降子序列 (数列)
题面 题解 调了好几个小时啊--话说我考试的时候脑子里到底在想啥-- 首先,这个数列肯定是有循环节的,而且循环节的长度\(T\)不会超过\(D\) 那么就可以把数列分成三份,\(L+S+R\),其中\ ...
- spring中使用缓存
一.启用对缓存的支持 Spring 对缓存的支持最简单的方式就是在方法上添加@Cacheable和@CacheEvict注解, 再添加注解之前,必须先启用spring对注解驱动的支持,基于java的配 ...
- Linq中DeferredLoadingEnabled,DataLoadOption的用法
1. 基本的数据关系图 Student和Class之间是多对一关系,Student和Course之间是多对多关系. DataContext的DeferredLoadingEnabled属性指定是否需 ...
- csv HTTP简单表服务器
HTTP Simple Table Server Download Performance testing with JMeter can be done with several JMeter in ...
- HDU 3652 区间有13并且这样整除13 的数量(数位DP)
题目:求1-n的范围里含有13且能被13整除的数字的个数. 分析: dfs(len, num, mod, flag) mod记录数字对13取余后的值 len表示当前位数 num==0 不含13且上一位 ...
- 一个基于QT简单登录对话框(带验证码功能)
1. 对话框样式 2. 源代码 ①. main.cpp #include <QtGui/QApplication> #include "QLoginDialog.h" ...
- 增加tomcat多实例
第一步:解压 第二步:修改端口 /data/service/tomcat1/conf <Server port="8006" shutdown="SHUTDOWN& ...