题意:求树的重心,若有多个,全部打印出来。

思路:

  树的重心:在删除点v后,森林中的每棵树的节点数尽量均匀,若最大的那棵树的节点数最小,称v为树的重心。

  这道题只是求树的所有重心,当且经当这棵树有对称性质时才有多重心,因此一棵树的重心最多不会超过2个。也是一遍DFS就可以搞定了,参考这个。

 //#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <deque>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define INF 0x7f7f7f7f
#define LL long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
int n, cnt, edge_cnt, head[N]; struct node
{
int from, to, next;
node(){};
node(int from,int to,int next):from(from),to(to),next(next){};
}edge[N*]; void add_node(int from,int to)
{
edge[edge_cnt]=node(from,to,head[from]);
head[from]=edge_cnt++;
}
deque<int> que; int DFS(int t,int far)
{
node e;
int big=, sum=;
for(int i=head[t]; i!=-; i=e.next)
{
e=edge[i];
if(e.to==far) continue; int tmp=DFS(e.to, t);
big=max(big, tmp);
sum+=tmp;
}
big=max(big, n-sum-); if(big<=cnt)
{
if(big<cnt) que.clear();
cnt=big;
que.push_back(t);
}
return sum+;
} int main()
{
//freopen("input.txt", "r", stdin);
int a, b;
while(~scanf("%d",&n))
{
edge_cnt=;
cnt=INF;
memset(head,-,sizeof(head)); for(int i=; i<n; i++)
{
scanf("%d%d",&a,&b);
add_node(a,b);
add_node(b,a);
}
DFS(,-);
sort(que.begin(),que.end());
while(!que.empty())
{
printf("%d ",que.front());
que.pop_front();
}
puts("");
}
return ;
}

AC代码

POJ 3107 Godfather (树的重心)的更多相关文章

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

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

  2. poj 3107 Godfather(树的重心)

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7885   Accepted: 2786 Descrip ...

  3. POJ 1655 BalanceAct 3107 Godfather (树的重心)(树形DP)

    参考网址:http://blog.csdn.net/acdreamers/article/details/16905653   树的重心的定义: 树的重心也叫树的质心.找到一个点,其所有的子树中最大的 ...

  4. Poj 2599 Godfather(树的重心)

    Godfather Time Limit: 2000MS Memory Limit: 65536K Description Last years Chicago was full of gangste ...

  5. POJ 3107 Godfather (树重心)

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

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

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

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

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

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

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

  9. POJ 3107 Godfather(树的重心)

    嘟嘟嘟 题说的很明白,就是求树的重心. 我们首先dfs一遍维护每一个点的子树大小,然后再dfs一遍,对于一个点u,选择子树中size[v]最小的那个和n - size[u]比较,取最大作为删除u后的答 ...

随机推荐

  1. Nhibernate中多Or条件的查询,很多Or的查询

    public IList<object[]> GetRequestAllByUserCodeUnitSysClassify1(string unitNo, string system, s ...

  2. [转]windows10 1703 鼠标右键打开命令提示符cmd

    https://answers.microsoft.com/zh-hans/windows/forum/windows_10-performance/windows10-1703/8bdfdfea-4 ...

  3. 爬虫库之BeautifulSoup学习(一)

    Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据. 官方解释如下: Beautiful Soup提供一些简单的.pytho ...

  4. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  5. Junit使用注意点

    注意点 1. 使用了@BeforeClass后@Ignore将会失效

  6. C/C++内存检测工具Valgrind

    内存检测Valgrind简介 Valgrind是运行在Linux上一套基于仿真技术的程序调试和分析工具,作者是获得过Google-O'Reilly开源大奖的Julian Seward, 它包含一个内核 ...

  7. IOS实时监控上传下载速度

    在开发中要获取网络类型是很简单的,导入Reachability直接获取状态就行了,现在我们要做一个类似下载器的那种实时把上传下载速度显示出来. 需要用到的头文件 使用Reachability 要测速度 ...

  8. OpenCV第二课,嵌入MFC

    单纯显示图片,只需链接 opencv_core2411d.libopencv_highgui2411d.lib 1.首先创建一个MFC的dlg,添加Picture Control控件 void CCO ...

  9. 解决overflow: auto在Ios中滑动不流畅

    [bug]—— H5页面在 ios 端滑动不流畅的问题   IOS系统的惯性滑动效果非常6,但是当我们对div加overflow-y:auto;后是不会出这个效果的,滑动的时候会感觉很生涩.怎么办? ...

  10. profiler Reserved Total

    Used Total和Reserved 均是物理内存,其中Reserved是unity向系统申请的总内存,Unity底层为了不经常向系统申请开辟内存,开启了较大一块内存作为缓存,即所谓的Reserve ...