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. C语言和sh脚本的杂交代码

    在网上看到了一个把 C语言和bash杂并起来的例子,这个示子如下所示.在下面这个例子中,我们把脚本用#if 0这个预编译给起来,这样就不会让其编译到C语言中了. #if 0 echo "He ...

  2. vm安装centos后unknown host问题和yum install安装不成功问题

    网上差了很多说要在vi /etc/sysconfig/network新增GATEWAY=192.168.0.1 还有vi /etc/sysconfig/network-scripts/ifcfg-et ...

  3. 常用linux基础命令(持续更新...)

    删除 空目录 rmdir非空目录 rm -rf 目录名字-r 就是向下递归,不管有多少级目录,一并删除-f 就是直接强行删除,不作任何提示的意思 删除文件命令rm -f 文件名将会强行删除文件,且无提 ...

  4. 【三支火把】--- 关于UEFI&PCD的总结介绍

    1个人理解 个人理解PCD基本等同于Token,应当算是一种描述性语言,按照规定书写好PCD的配置档,在编译的过程中,会根据你的配置生成同等含义的C文档,而在C文档中对应会出现相应的define或者变 ...

  5. Tinkphp 教程 一

    1项目生成配置php环境变量在控制台进入项目目录,执行php console build --config build.php命令在application目录创建项目目录,把创建好的目录复制到自定义a ...

  6. 同源策略与 JSONP CORS

    同源策略与 JSONP CORS 同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以 ...

  7. POJ 3252 区间内一个数的二进制中0的数量要不能少于1的数量(数位DP)

    题意:求区间内二进制中0的数量要不能少于1的数量 分析:很明显的是数位DP: 菜鸟me : 整体上是和数位dp模板差不多的 , 需要注意的是这里有前导零的影响 , 所以需要在dfs()里面增加zor ...

  8. js Form表单转json格式,及后台接收(多种方法)

    转载:https://blog.csdn.net/qq_40138785/article/details/81533015 一.serialize()方法格式:var data = $("# ...

  9. Django易混淆问题

    1.Django本身提供了runserver 为什么不能用来 部署 runserver 方法是调试 Django 时经常用到的运行方式,它使用 Django 自带的  WSGI Server 运行,主 ...

  10. 6-----BBS论坛

    BBS论坛(六) 6.1.优化json数据的返回 (1)新建utils/restful.py # utils/restful.py from flask import jsonify class Ht ...