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 树重心的更多相关文章

  1. POJ 1655 Balancing Act&&POJ 3107 Godfather(树的重心)

    树的重心的定义是: 一个点的所有子树中节点数最大的子树节点数最小. 这句话可能说起来比较绕,但是其实想想他的字面意思也就是找到最平衡的那个点. POJ 1655 题目大意: 直接给你一棵树,让你求树的 ...

  2. POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)

    关于树的重心:百度百科 有关博客:http://blog.csdn.net/acdreamers/article/details/16905653 1.Balancing Act To POJ.165 ...

  3. # [Poj 3107] Godfather 链式前向星+树的重心

    [Poj 3107] Godfather 链式前向星+树的重心 题意 http://poj.org/problem?id=3107 给定一棵树,找到所有重心,升序输出,n<=50000. 链式前 ...

  4. poj 3107 Godfather 求树的重心【树形dp】

    poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...

  5. poj 1741 树的点分治(入门)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18205   Accepted: 5951 Description ...

  6. POJ 1655 Balancing Act && POJ 3107 Godfather

    题目大意: 根据题目的图很好理解意思,就是记录每一个点的balance,例如 i 的balance就是把 i 从这棵树中除去后得到的森林中含有结点数最多 的子树中的节点个数,然后找到所有节点中对应的b ...

  7. cogs 1583. [POJ 3237] 树的维护 树链剖分套线段树

    1583. [POJ 3237] 树的维护 ★★★★   输入文件:maintaintree.in   输出文件:maintaintree.out   简单对比时间限制:5 s   内存限制:128 ...

  8. POJ 3107 Godfather (树重心)

    题目链接:http://poj.org/problem?id=3107 题意: 数重心,并按从小到大输出. 思路: dfs #include <iostream> #include < ...

  9. 树形dp&&树的重心(D - Godfather POJ - 3107)

    题目链接:https://cn.vjudge.net/contest/277955#problem/D 题目大意:求树的重心(树的重心指的是树上的某一个点,删掉之后形成的多棵树中节点数最大值最小). ...

随机推荐

  1. 787. Cheapest Flights Within K Stops

    There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...

  2. Mole and Abandoned Mine

    Mole and Abandoned Mine n点m条边的无向图,删除第i条边花费c[i],问1到n只有一条路径时所需要的最小花费? \(2\le n\le 15\) . 我又A掉了一道zzs的题啦 ...

  3. jdbcType DATE和TIMESTAMP区别

    当传入null值时,jdbcType 会防止null空指针异常报错 Mybatis 中 jdbcType =DATE,日期精确到年月日 剩余分秒 填0补齐 jdbcType= TIMESTAMP,日期 ...

  4. mysql的时区错误问题

    JDBC连接数据库报错如下: java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or r ...

  5. Xcode上传appstore 出现 Found an unexpected Mach-O header code: 0x72613c21 错误

    网上说是静态库的问题

  6. C语言中结构、联合、枚举的说明

    复杂的数据类型 一般的步骤: 1.声明模板 2.定义变量,分配内存空间 3.初始化 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  7. P2905 [USACO08OPEN]农场危机Crisis on the Farm

    传送门 DP 设 f [ i ] [ j ] [ k ] 表示已经走了 i 步,向上走了 j 步,向右走了 k 步时能拯救的最多奶牛数(j,k可以为负,表示反向) 设 g [ i ] [ j ] 表示 ...

  8. nginx与 Keepalived高可用

    1.1 keepalived软件能干什么? Keepalived软件起初是专为LVS负载均衡软件设计的, 用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能 K ...

  9. SqlServer自动备份作业

    /*********完整备份作业*********/ –完整备份,每周一次 USE Master GO declare @str varchar(100) set @str='D:\DBtext\jg ...

  10. jdk collections map类图