关于树的重心:百度百科

有关博客:http://blog.csdn.net/acdreamers/article/details/16905653

1.Balancing Act

To POJ.1655 Balancing Act

题目大意:

  有t组数据。每组数据给出n个点和n-1条边,构成一棵树,求该树的重心及删掉该点后形成的每棵子树的节点数。

代码:

 #include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=; int n,cnt,H[N<<],Ans,size,son[N];
bool vis[N];
struct Edge
{
int to,nxt;
}e[N<<]; void read(int &now)
{
now=;char c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c))now=(now<<)+(now<<)+c-'',c=getchar();
} void AddEdge(int u,int v)
{
e[++cnt].to = v;
e[cnt].nxt = H[u];
H[u] = cnt;
} void Init()
{
Ans=size=0x7fffffff;
for(int i=;i<=n;++i)
vis[i]=son[i]=H[i]=;
cnt=;
read(n);
int a,b;
for(int i=;i<n;++i)
read(a),read(b),AddEdge(a,b),AddEdge(b,a);
} void DFS(int cur)
{
vis[cur]=;
son[cur]=;
int tmp=;
for(int i=H[cur];i;i=e[i].nxt)
{
int to=e[i].to;
if(!vis[to])
{
DFS(to);
son[cur]+=son[to]+;
tmp=max(tmp,son[to]+);
}
}
tmp=max(tmp,n-son[cur]-);
if(size>tmp ||size==tmp&&Ans>cur)
{
Ans=cur;
size=tmp;
}
} int main()
{
int t;
read(t);
while(t--)
{
Init();
DFS();
printf("%d %d\n",Ans,size);
}
return ;
}

Balancing Act

2.Godfather

To POJ.3107 Godfather

题目大意:

  按升序输出该树的重心。

代码:

 #include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=; int n,cnt,tot,size,H[N<<],Ans[N],son[N];
bool vis[N];
struct Edge
{
int to,nxt;
}e[N<<]; void read(int &now)
{
now=;char c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c))now=(now<<)+(now<<)+c-'',c=getchar();
} void AddEdge(int u,int v)
{
e[++cnt].to = v;
e[cnt].nxt = H[u];
H[u] = cnt;
} void Init()
{
memset(H,,sizeof H);
memset(vis,,sizeof vis);
memset(Ans,0x3f,sizeof Ans);
size=0x7fffffff;
cnt=tot=;
read(n);
int x,y;
for(int i=;i<n;++i)
read(x),read(y),AddEdge(x,y),AddEdge(y,x);
} void DFS(int cur)
{
vis[cur]=;
son[cur]=;
int tmp=;
for(int i=H[cur];i;i=e[i].nxt)
{
int to=e[i].to;
if(!vis[to])
{
DFS(to);
son[cur]+=son[to]+;
tmp=max(tmp,son[to]+);
}
}
tmp=max(tmp,n-son[cur]-);
if(size>tmp)
{
size=tmp;
tot=;
Ans[tot]=cur;
}
else if(size==tmp)
{
Ans[++tot]=cur;
}
} int main()
{
Init();
DFS();
sort(Ans+,Ans++tot);
for(int i=;i<=tot;++i)
printf("%d ",Ans[i]);
return ;
}

Godfather

POJ.1655 Balancing Act POJ.3107 Godfather(树的重心)的更多相关文章

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

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

  2. POJ 1655 Balancing Act && POJ 3107 Godfather

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

  3. poj 1655 Balancing Act 求树的重心【树形dp】

    poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...

  4. poj 1655 Balancing Act(找树的重心)

    Balancing Act POJ - 1655 题意:给定一棵树,求树的重心的编号以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的. /* 找树的重心可以用树形dp或 ...

  5. POJ 1655 Balancing Act【树的重心】

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14251   Accepted: 6027 De ...

  6. POJ 1655.Balancing Act 树形dp 树的重心

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14550   Accepted: 6173 De ...

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

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

  8. POJ 1655 - Balancing Act 树型DP

    这题和POJ 3107 - Godfather异曲同工...http://blog.csdn.net/kk303/article/details/9387251 Program: #include&l ...

  9. POJ 1655 - Balancing Act - [DFS][树的重心]

    链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tre ...

随机推荐

  1. MySQL— pymysql and SQLAlchemy

    目录 一.pymysql 二.SQLAlchemy 一.pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 1. 下载安装 #在终端直接运行 pip ...

  2. shiro设置session超时时间

    系统默认超时时间是180000毫秒(30分钟) long timeout = SecurityUtils.getSubject().getSession().getTimeout(); System. ...

  3. 前端web服务器数据同步方案

    概述: 网站采用了web和mysql数据库分离的架构,前端有web1.web2.web3需要对他们进行上传文件同步 方案: 在web2的windows服务器上安装GoodSync软件,利用其双向同步特 ...

  4. 配置spring所需要的jar包

    spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-m ...

  5. urllib处理包的简单使用

    我们可以使用urllib.request.urlopen()这个接口函数就可以打开一个网站,读取打印信息 你可以现在终端使用python from urllib import request if _ ...

  6. vue系列之webstrom的设置

    1.安装vue插件,方法 Setting->Plugins,点击Plugins,在右边输入vue,找到相应插件,然后安装 2.创建vue模板 注意红圈里面的 3.设置vue文件支持的样式 注意: ...

  7. 输入年月日判断是当年第几天(if判断)

  8. NodeJs>------->>第二章:Node.js中交互式运行环境--------REL

    第二章:Node.js中交互式运行环境--------REL 一:REPL运行环境概述 C:\Users\junliu>node > foo = 'bar' ; 'bar' > 二: ...

  9. Windows环境selenium+Python环境配置

    1.安装Python 访问Python官方网站. 根据自己的操作系统32/64 位,选择相应的版本. 安装过程我就不详细描述了,动动手指头,Google一下,你就知道.我的安装目录为:C:\Pytho ...

  10. uva11610 树状数组+素数打表求因子,好题!

    /* uva11610 树状数组+素数打表+离散化 打出的素数范围在2-1000000之间,不超过六位数,然后按照格式翻转成七位数 */ #include<bits/stdc++.h> u ...