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会超时,看了以后还是用邻接吧. 题意: 给一颗树,保证是一颗树,求去掉一个点以后的联通块里节 ...
随机推荐
- Xcode中需要熟悉的常用快捷键
因为工作需要,笔者最近开始接触Xcode这款Mac系统下的强大的编程软件.因为个人习惯,每当接触新的软件的时候总会先去了解它的一些常用快捷键.经过多方查阅总结出以下内容,希望对刚刚接触Xcode的初学 ...
- 不同浏览器下css 透明度的写法
filter:alpha(opacity=90); /* IE transparent */ -moz-opacity:0.9; /* Moz + FF transparent */ opacity: ...
- 暑假集训(1)第七弹 -----Oil Deposits(Poj1562)
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- leetcode344——Reverse String(C++)
Write a function that takes a string as input and returns the string reversed. Example:Given s = &qu ...
- HttpUtility.HtmlEncode
HttpUtility.HtmlEncode用来防止站点受到恶意脚本注入的攻击 public string Welcome(string name, int numTimes = 1) { r ...
- c#解决数据库用in的时候大于1000报错问题
问题: //oracle数据库报错 ,,,,,,,.....); c#写了一个方法解决 /// <summary> /// 拼接sql /// </summary> /// & ...
- 隐私:网民最常用密码MD5解密
国内知名网络安全商瑞星公司曾发布过一项针对密码强度的专业研究报告,这项研究中列举了中国网民和美国网民最常用的密码集.研究表明,全球互联网大部分用户在密码使用中都存在着种种疏漏,一些极其简单的密码被广泛 ...
- Hibernate Session 获取connection
Hibernate Session 获取connection 由于最近一个项目要用到一条辅助的SQL ,hibernate里面的SQLQuery API 总的SQL语句不能包含 : 冒号, 固放弃Hi ...
- front-end
http://info.1688.com/detail/1139720782.html http://segmentfault.com/q/1010000000136513 http://h5apps ...
- angular2 学习笔记 ( Router 路由 )
参考 : https://angular.cn/docs/ts/latest/guide/router.html#!#can-activate-guard https://angular.cn/doc ...