关于树的重心:百度百科

有关博客: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. concurrent.futures- 启动并行任务

    python因为其全局解释器锁GIL而无法通过线程实现真正的平行计算.这个论断我们不展开,但是有个概念我们要说明,IO密集型 vs. 计算密集型. IO密集型:读取文件,读取网络套接字频繁. 计算密集 ...

  2. saltstack自动化运维系列⑤之saltstack的配置管理详解

    saltstack自动化运维系列⑤之saltstack的配置管理详解 配置管理初始化: a.服务端配置vim /etc/salt/master file_roots: base: - /srv/sal ...

  3. WebMvcConfigurerAdapter已过时

    Spring Boot2.0的版本(创建的时候自动选择的这个版本),然后编译器告诉我WebMvcConfigurerAdapter已过时了 @Deprecated public abstract cl ...

  4. ocp linux 基础要点

    基本命令: 创建/修改/删除用户    useradd/usermod/userdel 创建/修改/删除用户组    groupadd/groupmod/groupdel    修改所属用户/所属用户 ...

  5. dispatchers 设置

    Oracle连接方式(dispatchers 设置) oracle 响应客户端请求有两种方式: 1 专有连接:用一个服务器进程响应一个客户端请求 2 共享连接:用一个分派器(dispatcher)响应 ...

  6. js常用函数整理

    类型转换:parseInt\parseFloat\toString 类型判断:typeof;eg:if(typeof(var)!="undefined")\isNaN 字符处理函数 ...

  7. CNN中各种各样的卷积

    https://zhuanlan.zhihu.com/p/29367273 https://zhuanlan.zhihu.com/p/28749411 以及1*1卷积核:https://www.zhi ...

  8. linux 下 eclipse 安装

    下载: 官网选择相应安装包下载,我这里下了tar.gz包 安装: tar xzvf eclipse-inst-linux64.tar.gz 设置环境变量 export JAVA_HOME=/usr/l ...

  9. iOS学习笔记之UITableViewController&UITableView

    iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...

  10. Chakra GC内存管理(未完)

    这一部分是我在网上找Chakra资料的时候偶然发现的zenhumany师傅在Hitcon2015上的议题<Microsoft Edge MemGC Internals>,感觉正好可以了解一 ...