# 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

6 6 6

Sample Output

5 2

2 5

4 1

6 0
HINT

program Meet;
type
point=^node;
node=record
x:longint; next:point;
end;
var
a:array[0..500000]of point;
f:array[0..20,0..500000]of longint;
deep,s:array[0..500000]of longint;
n,i,m,x,y,t,z,v1,v2,v3,ans:longint;
var p:point;
begin
new(p); p^.x:=y; p^.next:=a[x]; a[x]:=p;
end;
procedure dfs(x,k:longint);
var p:point;
begin
new(p); p:=a[x];  f[0,x]:=k; deep[x]:=deep[k]+1;
while p<>nil do
begin
if p^.x<>k then dfs(p^.x,x);p:=p^.next;
end;
end;
procedure work;
var i,j:longint;
begin
for i:=0 to trunc(ln(n)/ln(2))-1 do
for j:=1 to n do
f[i+1,j]:=f[i,f[i,j]];
end;
function lca(x,y:longint):longint;
var i,t:longint;
begin
if deep[x]<deep[y] then begin t:=x; x:=y; y:=t; end;
for i:=0 to trunc(ln(n)/ln(2)) do
if ((deep[x]-deep[y])>>i) and 1=1 then x:=f[i,x];
if x=y then exit(x);
for i:=trunc(ln(n)/ln(2)) downto 0 do
if f[i,x]<>f[i,y] then
begin x:=f[i,x]; y:=f[i,y]; end;
exit(f[0,x]);
end;
begin
assign(input,'Meet.in');
reset(input);
assign(output,'Meet.out');
rewrite(output);
for i:=1 to n-1 do
begin
end;
deep[1]:=0;
dfs(1,1);  work;
for i:=1 to m do
begin
v1:=lca(x,y);  v2:=lca(x,z); v3:=lca(z,y);
if (v1=v2)and(v2=v3) then t:=v1
else if v1=v2 then t:=v3
else if v2=v3 then t:=v1
else t:=v2;
v1:=lca(x,t); v2:=lca(y,t); v3:=lca(z,t);
ans:=deep[x]+deep[y]+deep[z]+deep[t]*3-2*(deep[v1]+deep[v2]+deep[v3]);
writeln(t,' ',ans);
end;
close(input); close(output);
end.
View Code

posted @ 2016-09-16 16:41  QTY_YTQ  阅读(175)  评论(0编辑  收藏  举报