1787: [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合
Time Limit: 20 Sec Memory Limit: 162 MB
Submit: 1482 Solved: 652
[Submit][Status][Discuss]
Description

Input

Output

Sample Input
1 2
2 3
2 4
4 5
5 6
4 5 6
6 3 1
2 4 4
6 6 6
Sample Output
2 5
4 1
6 0
HINT

Source
题解:很明显,两个人的时候,最优方案即为两者的LCA;而三个点时,必然是某两个点的LCA,然后求出两两的LCA,然后判断即可= =,直接水过
/**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ type
point=^node;
node=record
g:longint;
next:point;
end;
var
i,j,k,l,m,n,a1,a2,a3,a4,a5,a6:longint;
a:array[..] of point;
b:array[..] of longint;
c:array[..,..] of longint;
d:array[..,..] of longint;
function min(x,y:longint):longint;
begin
if x<y then min:=x else min:=y;
end;
function max(x,y:longint):longint;
begin
if x>y then max:=x else max:=y;
end;
function max3(x,y,z:longint):longint;
begin
exit(max(max(x,y),z));
end;
procedure swap(var x,y:longint);
var z:longint;
begin
z:=x;x:=y;y:=z;
end;
procedure add(x,y:longint);
var p:point;
begin
new(p);p^.g:=y;p^.next:=a[x];a[x]:=p;
end;
procedure dfs(y,x:longint);
var p:point;
begin
p:=a[x];
while p<>nil do
begin
if p^.g<>y then
begin
b[p^.g]:=b[x]+;
c[,p^.g]:=x;
dfs(x,p^.g);
end;
p:=p^.next;
end;
end;
function getfat(x,y:longint):longint;
var i:longint;
begin
i:=;
while y> do
begin
if odd(y) then x:=c[i,x];
inc(i);y:=y div ;
end;
exit(x);
end;
function getcom(x,y:longint):longint;
var i:longint;
begin
if b[x]<b[y] then swap(x,y);
x:=getfat(x,b[x]-b[y]);
if x=y then exit(x);
for i:=trunc(ln(b[x])/ln()+) downto do
if c[i,x]<>c[i,y] then
begin
x:=c[i,x];
y:=c[i,y];
end;
exit(c[,x]);
end;
function dis(x,y:longint):longint;
var z:longint;
begin
z:=getcom(x,y);
exit(b[x]-b[z]+b[y]-b[z]);
end;
procedure sort(l,r:longint);
var i,j,x,y:longint;
begin
i:=l;j:=r;x:=d[(l+r) div ,];y:=d[(l+r) div ,];
repeat
while (d[i,]<x) do inc(i);
while (d[j,]>x) do dec(j);
if i<=j then
begin
swap(d[i,],d[j,]);
swap(d[i,],d[j,]);
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if l>j then sort(l,j);
end;
begin
readln(n,m);
for i:= to n do a[i]:=nil;
for i:= to n- do
begin
readln(j,k);
add(j,k);add(k,j);
end;
l:=random(n)+;b[l]:=;
dfs(,l);
for i:= to trunc(ln(n)/ln()+) do
for j:= to n do
c[i,j]:=c[i-,c[i-,j]];
for i:= to m do
begin
readln(j,k,l);
a1:=getcom(j,k);
a2:=getcom(k,l);
a3:=getcom(j,l);
a4:=dis(a1,l);
d[,]:=(b[j]-b[a1])+(b[k]-b[a1])+a4;
d[,]:=a1;
a5:=dis(a2,j);
d[,]:=a5+(b[k]-b[a2])+(b[l]-b[a2]);
d[,]:=a2;
a6:=dis(a3,k);
d[,]:=(b[j]-b[a3])+a6+(b[l]-b[a3]);
d[,]:=a3;
sort(,);writeln(d[,],' ',d[,]);
end;
end.
1787: [Ahoi2008]Meet 紧急集合的更多相关文章
- bzoj 1787 [Ahoi2008]Meet 紧急集合(1832 [AHOI2008]聚会)
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1841 Solved: 857[Submit][ ...
- BZOJ 1787: [Ahoi2008]Meet 紧急集合( 树链剖分 )
这道题用 LCA 就可以水过去 , 但是我太弱了 QAQ 倍增写LCA总是写残...于是就写了树链剖分... 其实也不难写 , 线段树也不用用到 , 自己YY一下然后搞一搞就过了...速度还挺快的好像 ...
- bzoj 1787: [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 ...
- BZOJ 1787: [Ahoi2008]Meet 紧急集合 LCA
1787: [Ahoi2008]Meet 紧急集合 Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 ...
- BZOJ 1787: [Ahoi2008]Meet 紧急集合(lca+贪心)
[Ahoi2008]Meet 紧急集合 Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 2 4 4 ...
- BZOJ——1787: [Ahoi2008]Meet 紧急集合
http://www.lydsy.com/JudgeOnline/problem.php?id=1787 题目描述 输入 输出 样例输入 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 ...
- bzoj 1787: [Ahoi2008]Meet 紧急集合【树链剖分lca】
对于三个点求最小路径长度和,答案肯定在某两个点的lca上,因为如果把集合点定在公共lca上,一定有两个点汇合后再一起上到lca,这样显然不如让剩下的那个点下来 这个lca可能是深度最深的--但是我懒得 ...
- bzoj1787 [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 2272 Solved: 1029 [Submi ...
- [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBhttp://www.lydsy.com/JudgeOnline/p ...
随机推荐
- Failed to register Grid Infrastructure type ora.mdns.type
安装11g的集群软件的时候,在最后运行root.sh脚本时候,没有执行成功,最后提示如下错误: [root@r2 ~]# /u01/app/11.2.0/grid_1/root.sh Performi ...
- 记一次DG搭建过程中ORA-09925: Unable to createaudit trail file 错误
今天做Oracle DG 编写initorcl的时候,修改完以后,sqlplus就不能再登陆,一直报 ERROR: ORA-09925: Unable to createaudit trail fi ...
- MyBatis中的大于、小于、like等符号写法
其实就是xml的特殊符号,因为它的配置就是xml,所以可以用下面这种写法转义 < < > > <> <> & & ' ...
- SQL语句中日期的计算方法大全
sql语句中的日期处理一.日期處理函數1.日期增減函數 dateadd(datepart,number,dtae) datepart:是規定應向日期的哪一部分返回新值的參數.下列是sql server ...
- 支持Angular 2的表格控件
前端框架一直这最近几年特别火的一个话题,尤其是Angular 2拥有众多的粉丝.在2016年9月份Angular 2正式发布之后,大量的粉丝的开始投入到了Angular 2的怀抱.当然这其中也包括我. ...
- 微信小程序怎么做出前端table的效果
wxml代码: <view class="container"> <view class="table"> &l ...
- redis的配置详解
redis 127.0.0.1:6379> CONFIG GET loglevel 1) "loglevel" 2) "notice" Redis 的配置 ...
- hadoop在windows下安装运行
1.下载windows环境下编译的hadoop-2.7.2.x64win.zip 2.解压至D:\BigData\hadoop-2.7.2 3.修改D:\BigData\hadoop-2.7.2\et ...
- windows下Visual Studio Code 1.9安装
点击送你去官网下载vs code 进入下载页面如下 下载完成双击安装 安装完成打开之后如下图,最新版的默认中文就挺好的! 注意下面是微软建议: Note: .NET Framework 4.5.2 i ...
- SwingBench---ORACLE压力测试工具
SwingBench---ORACLE压力测试工具 ◆描述SwingBench是Oracle UK的一个员工在一个被抛弃的项目的基础上开发的.目前稳定版本2.5,基于JDK.该工具是免费的,可以在作者 ...