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)为其中的最大值,点 ...
随机推荐
- 20155317 实验二 Java面向对象程序设计
20155317 实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ...
- 20155325实验四 Android程序设计
实验四 Android程序设计-1 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)&g ...
- 20145209 2016-2017-2 《Java程序设计》课程总结
20145209 2016-2017-2 <Java程序设计>课程总结 每周作业链接汇总 预备作业01 http://www.cnblogs.com/liuyiyang/p/6194319 ...
- 【原创】user.id字段
odoo中User.ID 字段是用户登录表 res_users 中的字段,所以要关联某个用户或是判断某个用户,可以利用该字段. 例如:在某个 界面中的domain中,要求显示的是关联某用户的单子,则如 ...
- 如何写一个简单的HTTP服务器(重做版)
最近几天用C++重新写了之前的HTTP服务器,对以前的代码进行改进.新的HTTP服务器采用Reactor模式,有多个线程并且每个线程有一个EventLoop,主程序将任务分发到每个线程,其中采用的是轮 ...
- 我们一起学习WCF 第十篇Wcf中实现事务
数据一致性在工作中显得非常重要,有时候我们库中出现脏数据导致程序报错,但是又很难发现这样的错误,所以为了数据的完整性建议在程序中加入事物. 什么是事物:我们都有团队合作吧,比喻团队有3个人,a负责设计 ...
- Redis的事物
Redis的事物 Redis 事物常用命令 multi标记一个事物块的开始 exec:执行所有事物块内的命令 discard: 取消事物,放弃执行事物块的所有命令 watch key [k ...
- 第五篇 Postman离线安装interceptor插件---Chrome app及录制请求
在测试中,总是苦恼于无法直接使用chrome浏览器的cookie等信息,终于在茫茫网海中,发现了 https://www.jianshu.com/p/a4223bab1e73, 感谢 智者向内寻求力量 ...
- 学习笔记之ubuntu修改固定IP脚本
一.shell脚本编程 二.正则表达式 三.linux修改IP的方法 #!/bin/bash cd /etc/network/ stty erase '^?' write_interfaces() { ...
- 面向英特尔® x86 平台的 Unity* 优化指南: 第 1 部分
原文地址 目录 工具 Unity 分析器 GPA 系统分析器 GPA 帧分析器 如要充分发挥 x86 平台的作用,您可以在项目中进行多种性能优化,以最大限度地提升性能. 在本指南中,我们将展示 Uni ...