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会超时,看了以后还是用邻接吧. 题意: 给一颗树,保证是一颗树,求去掉一个点以后的联通块里节 ...
随机推荐
- iOS支付宝集成步骤;王刚韧的技术博客
- OC中的字符串常用方法
OC中的字符串常用方法 OC中对字符串进行操作使用了Foundation框架中的NSString类(不可变).NSMutableString类(可变). NSString 1.创建字符串 [objc] ...
- ReactNative-----环境搭建二(android)
一.初始化一个ReactNative项目 在指定目录运行命令:react-native init Vince(项目名称) //其过程就是在使用CLI工具构建项目, 命令行代码 F:\React> ...
- ffmpeg只使用h264编译参数
--disable-everything --enable-decoder=h264 --enable-demuxer=h264 --enable-parser=h264 --disable-ffpl ...
- Sql server 浅谈用户定义表类型
1.1 简介 SQL Server 中,用户定义表类型是指用户所定义的表示表结构定义的类型.您可以使用用户定义表类型为存储过程或函数声明表值参数,或者声明您要在批处理中或在存储过程或函数的主体中使用的 ...
- PHP——图片上传
图片上传 Index.php文件代码: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- HTML注释的一些规范
HTMl里的一些注释符号 1.bady,head内部的注释:<!--放注释内容--> 2.css样式的注释:/*放注释的内容*/ 3.javascript注释 单行注释://放注释的内容 ...
- spring的基本配置
一:web.xml (1)spring mvc的配置 <servlet> <description>spring mvc servlet</description> ...
- wap开发笔记之幻灯片
最近在进行wap站研究,发现网上成熟的wap幻灯片都很难找到,在此贴出一个iphone的幻灯效果,希望对wap站开发的人有些帮助. 点此下载
- Jquery 实现瀑布流布局
//保证img文件夹下有图片//引入jquery <script src="Script/jquery-1.7.2.js"></script> <st ...