POJ 3107
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define MAXN 50010
using namespace std;
typedef struct PP{
int id,par;
bool operator < (const PP & a) const{
if(par == a.par) return id < a.id;
return par < a.par;
}
}Partition;
typedef struct{
int to,next;
}Node;
Node edge[*MAXN];
Partition P[MAXN];
int head[MAXN],vis[MAXN],cnt[MAXN],N;
void addedge(int u,int v,int k){
edge[k].to = v;
edge[k].next = head[u];
head[u] = k;
}
int dfs_child(int s){
vis[s] = cnt[s] = ;
for(int i = head[s];~i;i = edge[i].next){
int u = edge[i].to;
if(!vis[u]) cnt[s] += dfs_child(u);
}
return cnt[s]--;
}
void dfs_partition(int s){
P[s].id = s;
P[s].par = N-cnt[s]-;
vis[s] = ;
for(int i = head[s];~i;i = edge[i].next){
int u = edge[i].to;
if(!vis[u]){
P[s].par = max(P[s].par,cnt[u]+);
dfs_partition(u);
}
}
}
int main(){
int u,v;
while(~scanf("%d",&N)){
int k = ;
memset(head,-,sizeof(head));
for(int i = ;i <= N-;i ++){
scanf("%d%d",&u,&v);
addedge(u,v,k++);
addedge(v,u,k++);
}
memset(vis,,sizeof(vis));
dfs_child();
memset(vis,,sizeof(vis));
dfs_partition();
sort(P+,P+N+);
k = ;
while(P[k].par == P[].par){
printf("%d ",P[k].id);
k++;
}
printf("\n");
}
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 1655 Balancing Act && POJ 3107 Godfather
题目大意: 根据题目的图很好理解意思,就是记录每一个点的balance,例如 i 的balance就是把 i 从这棵树中除去后得到的森林中含有结点数最多 的子树中的节点个数,然后找到所有节点中对应的b ...
- # [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 树形dp
Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7536 Accepted: 2659 Descrip ...
- poj 3107 删点最大分支最小
http://poj.org/problem?id=3107 这实际上就是找重心,在之前有做过:http://www.cnblogs.com/qlky/p/5780933.html #include ...
- POJ 3107 Godfather (树重心)
题目链接:http://poj.org/problem?id=3107 题意: 数重心,并按从小到大输出. 思路: dfs #include <iostream> #include < ...
- poj 3107 树重心
http://acm.hust.edu.cn/vjudge/problem/18069 和poj 1655差不多:http://www.cnblogs.com/qlky/p/5780933.html ...
- POJ 3107 Godfather (树形dp)
题目链接 虽然题目不难,但是1A还是很爽, 只是刚开始理解错题意了,想了好久. 还有据说这个题用vector会超时,看了以后还是用邻接吧. 题意: 给一颗树,保证是一颗树,求去掉一个点以后的联通块里节 ...
随机推荐
- zabbix 配置
终于把zabbix配置好了.可能还有待优化 我主要参考了几个链接 http://lnmp.org/install.html 一键安装lnmp http://blog.unix178.com/2 ...
- Linux下的visudo命令
新建了一个test账号,su 后使用visudo命令,发现不可用,退出putty,直接用root登录,可以用了,小白了,visudo只能用root执行,简单写下visudo命令吧 visudo后得到的 ...
- qwt6在Windows下Qt5的编译,安装,初步使用
今晚把qwt的编译,安装,初级使用放上来,以便需要的人,能更快部署好编程环境,不至于每次都像我这样花很多时间. 注意:Qtcreater使用的是什么编译器编译出来的,就要用那个编译器来编译qwt. 我 ...
- 基于IAccessible接口的QQ窗口信息获取的实现
这个技术现在已经封装成DLL免费开放给大家使用了,详情请加群221487171 可以访问官方网站下载 http://www.guihelper.com/ 主要技术(Microsoft Active A ...
- 关于datagridview单元格不切换焦点无法获得新输入数据的问题解决方法
问题描述:将EXCEL中的数据导入到dataGridView中,然后通过点击toolStripButton对dataGrideView中的数据进行处理,在测试时,向dataGridView中的某个单元 ...
- 方便mac os 10.9系统中phpstorm配置php运行环境
自己安装php,不用mac安装,这样就有php开发环境了. 安装很简单,直接运行一个命令, 需要几分钟,请慢慢等待. curl -s http://php-osx.liip.ch/install.sh ...
- 发布FireBird数据库所需要DLL文件
数据库版本:2.5.2 ib_util.dll; icudt30.dll; icuin30.dll icuuc30.dll
- VC释放EXE资源文件
原文地址:http://blog.csdn.net/wangningyu/article/details/4378378 今天有个朋友问到VC能否释放多个EXE.DLL或WAV等文件,我便做了个实例给 ...
- 数据采集服务提供商,ip提供商 里面有些不错的基础数据
http://user.qzone.qq.com/1649677458 这家公司的爬虫应该挺牛的 !@#!#!~#¥¥¥@@http://www.site-digger.com/
- settimeout vs setinternal
http://blog.sina.com.cn/s/blog_6b1ab3be0100pzmo.html http://www.360doc.com/content/11/0412/17/100779 ...