poj1655 Balancing Act求树的重心
Description
For example, consider the tree:

Deleting node 4 yields two trees whose member nodes are {5} and {1,2,3,6,7}. The larger of these two trees has five nodes, thus the balance of node 4 is five. Deleting node 1 yields a forest of three trees of equal size: {2,6}, {3,7}, and {4,5}. Each of these trees has two nodes, so the balance of node 1 is two.
For each input tree, calculate the node that has the minimum balance. If multiple nodes have equal balance, output the one with the lowest number.
Input
Output
Sample Input
1
7
2 6
1 2
1 4
4 5
3 7
3 1
Sample Output
1 2 这是一个模板题,是点分治的基础。我们先随便把一个结点作为根,求出siz数组,siz[i]表示以i为根的子树的大小(dfs/bfs都可以,我就用bfs了),然后去掉结点i后的最大块的大小就是max(n-siz[i],i的所有儿子中的最大siz),这样就做好了。
program rrr(input,output);
type
etype=record
t,next:longint;
end;
var
e:array[..]of etype;
a,q,father,siz,f:array[..]of longint;
v:array[..]of boolean;
tt,i,j,n,x,y,cnt,h,t,ans,min:longint;
function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end;
procedure add(x,y:longint);
begin
inc(cnt);e[cnt].t:=y;e[cnt].next:=a[x];a[x]:=cnt;
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
readln(tt);
for i:= to tt do
begin
readln(n);
for j:= to n do a[j]:=;cnt:=;
for j:= to n- do begin read(x,y);add(x,y);add(y,x); end;
fillchar(v,sizeof(v),false);
h:=;t:=;q[]:=;v[]:=true;
while h<t do
begin
inc(h);
j:=a[q[h]];
while j<> do
begin
if not v[e[j].t] then
begin
v[e[j].t]:=true;father[e[j].t]:=q[h];
inc(t);q[t]:=e[j].t;
end;
j:=e[j].next;
end;
end;
for j:= to n do siz[j]:=;
fillchar(f,sizeof(f),);min:=n;
for j:=n downto do
begin
t:=max(f[q[j]],n-siz[q[j]]);
if (t<min) or (t=min) and (q[j]<ans) then begin ans:=q[j];min:=t; end;
inc(siz[father[q[j]]],siz[q[j]]);
if siz[q[j]]>f[father[q[j]]] then f[father[q[j]]]:=siz[q[j]];
end;
if f[]<=min then begin ans:=;min:=f[]; end;
writeln(ans,' ',min);
end;
close(input);close(output);
end.
poj1655 Balancing Act求树的重心的更多相关文章
- poj1655 Balancing Act 找树的重心
http://poj.org/problem? id=1655 Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- poj 1655 Balancing Act 求树的重心【树形dp】
poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...
- POJ 1655 Balancing Act(求树的重心--树形DP)
题意:求树的重心的编号以及重心删除后得到的最大子树的节点个数size,假设size同样就选取编号最小的. 思路:随便选一个点把无根图转化成有根图.dfs一遍就可以dp出答案 //1348K 125MS ...
- POJ-1655 Balancing Act(树的重心)
Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...
- POJ1655 Balancing Act (树的重心)
求树的重心的模板题,size[u]维护以u为根的子树大小,f[u]表示去掉u后的最大子树. 1 #include<cstdio> 2 #include<iostream> 3 ...
- POJ1655 Balancing Act(树的重心)
树的重心即树上某结点,删除该结点后形成的森林中包含结点最多的树的结点数最少. 一个DFS就OK了.. #include<cstdio> #include<cstring> #i ...
- POJ 1655 Balancing Act (求树的重心)【树形DP】(经典)
<题目链接> 题目大意:给你一棵树,任意去除某一个点后,树被分成了几个联通块,则该点的平衡值为所有分成的连通块中,点数最大的那个,问你:该树所有点中,平衡值最小的那个点是什么? 解题分析: ...
- Balancing Act(树的重心)
传送门 Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14070 Accepted: 593 ...
- POJ 1655 Balancing Act (树的重心,常规)
题意:求树的重心,若有多个重心,则输出编号较小者,及其子树中节点最多的数量. 思路: 树的重心:指的是一个点v,在删除点v后,其子树的节点数分别为:u1,u2....,设max(u)为其中的最大值,点 ...
随机推荐
- 20145226夏艺华 《Java程序设计》实验报告四
实验四 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试 了解Android组件.布局管理器的使用 掌握Android中事件处理机制 Andro ...
- C#基础之并行编程
并行编程从业务实现的角度可分为数据并行与任务并行,也就是要解决的问题是以数据为核心还是以要处理的事情为核心.基于任务的并行编程模型TPL(任务并行库)是从业务角度实现的并行模型,它以System.Th ...
- RHSCA模拟考试
开始考试:桌面是个黑框子 点击reboot按钮,破解密码 开机成功,输入startx进入图形界面 不能复制,要在物理机用ssh root@172.25.0.11 远程连接,就可以复制粘贴了 * Hos ...
- springboot入门之一:环境搭建
springboot简介 springboot做为微服务的开发集合框架,有着天然的好处,它不像springmvc那样笨重繁杂,springmvc众多的配置使得开发人员很厌烦,为解决众多的配置带来的烦扰 ...
- iOS 小技巧
投影效果 scanBtn.layer.shadowColor = [UIColorblackColor].CGColor;//shadowColor阴影颜色 scanBtn.layer.sha ...
- SQL查找重复项目
1 2 3 4 5 6 7 SELECT t1.* FROM t1, (SELECT name,ADD FROM t1 GROUP BY name,ADD HAVING COUNT(1 ...
- 高可用Kubernetes集群-5. 部署flannel网络
七.部署flannel网络 kubernetes支持基于vxlan方式的flannel与weave网络,基于BGP路由的Calico网络,本节采用flannel网络. Flannel网络采用etcd等 ...
- Debian 给非 ROOT 用户添加 sudoer 权限
问题描述 从官方镜像安装的 Debian 9 (Stretch)比较纯净,但因此需要自己安装.配置许多常用的 Linux 应用,这里就需要 sudo (super user do)临时获取 root ...
- 面向对象编程(OOP)思想小结
Concepts 类(class):对我们要解决问题的抽象,比如建造房子的蓝图:但实现机制上来讲,类是根据蓝图构建而成的,存储在内存中的,用来表示对象的数据. 对象(object):根据类构建的实体, ...
- ES6的新特性(8)——数组的扩展
数组的扩展 扩展运算符 含义 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. console.log(...[1, 2, 3]) / ...