每个顶点有且仅有一条出边是什么意思呢

类似一棵树,树上的边都是由儿子指向父亲的,并且这个东西带着一个环

也就是一个个有向环套有向树……

这题还是比较简单的,把环作为根然后类似lca做即可,注意细节的panding

 type node=record
po,next:longint;
end; var e:array[..] of node;
s,p,w,c,be,d,q:array[..] of longint;
v:array[..] of boolean;
anc:array[..,..] of longint;
t,f,r,n,m,len,i,x,y,a,b:longint; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure add(x,y:longint);
begin
inc(len);
e[len].po:=y;
e[len].next:=p[x];
p[x]:=len;
end; procedure bfs;
var x,i,y:longint;
begin
f:=;
while f<=r do
begin
x:=q[f];
for i:= to do
begin
y:=anc[x,i-];
if y<> then anc[x,i]:=anc[y,i-] else break;
end;
i:=p[x];
while i<> do
begin
y:=e[i].po;
if not v[y] then
begin
d[y]:=d[x]+;
anc[y,]:=x;
be[y]:=t;
inc(r);
q[r]:=y;
v[y]:=true;
end;
i:=e[i].next;
end;
inc(f);
end;
end; procedure lca(x,y:longint);
var i,num,a1,b1,a2,b2:longint;
begin
a:=; b:=;
if d[x]>d[y] then
for i:= downto do
if d[x]- shl i>=d[y] then
begin
x:=anc[x,i];
a:=a+ shl i;
end; if d[y]>d[x] then
for i:= downto do
if d[y]- shl i>=d[x] then
begin
y:=anc[y,i];
b:=b+ shl i;
end; if x=y then exit;
for i:= downto do
if (anc[x,i]<>anc[y,i]) then
begin
x:=anc[x,i]; a:=a+ shl i;
y:=anc[y,i]; b:=b+ shl i;
end; if (anc[x,]=anc[y,]) and (anc[x,]<>) then
begin
inc(a); inc(b);
exit;
end;
num:=s[be[x]];
if c[x]>c[y] then a1:=a+num-c[x]+c[y] else a1:=a+c[y]-c[x];
b1:=b;
a2:=a;
if c[x]<c[y] then b2:=b+num-c[y]+c[x] else b2:=b+c[x]-c[y];
if max(a1,b1)=max(a2,b2) then
begin
if (min(a1,b1)>min(a2,b2)) or (min(a1,b1)=min(a2,b2)) and (a1<b1) then
begin
a:=a2;
b:=b2;
end
else begin
a:=a1;
b:=b1;
end;
end
else if max(a1,b1)>max(a2,b2) then
begin
a:=a2;
b:=b2;
end
else begin
a:=a1;
b:=b1;
end;
end; begin
readln(n,m);
for i:= to n do
begin
read(w[i]);
add(w[i],i);
end; for i:= to n do
if be[i]= then
begin
inc(t);
be[i]:=t;
x:=i;
while true do
begin
x:=w[x];
if be[x]=t then break;
be[x]:=t;
end;
y:=x;
r:=;
repeat
inc(s[t]);
c[y]:=s[t];
inc(r);
q[r]:=y;
v[y]:=true;
y:=w[y];
until y=x;
bfs;
end; for i:= to m do
begin
readln(x,y);
if be[x]<>be[y] then
begin
writeln('-1 -1');
continue;
end;
lca(x,y);
writeln(a,' ',b);
end;
end.

bzoj2791的更多相关文章

  1. 【BZOJ2791】[Poi2012]Rendezvous 倍增

    [BZOJ2791][Poi2012]Rendezvous Description 给定一个n个顶点的有向图,每个顶点有且仅有一条出边.对于顶点i,记它的出边为(i, a[i]).再给出q组询问,每组 ...

  2. [BZOJ2791][Poi2012]Rendezvous

    2791: [Poi2012]Rendezvous Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 95  Solved: 71[Submit][Sta ...

  3. BZOJ2791 Rendezvous

    Description给定一个n个顶点的有向图,每个顶点有且仅有一条出边.对于顶点i,记它的出边为(i, a[i]).再给出q组询问,每组询问由两个顶点a.b组成,要求输出满足下面条件的x.y:1. ...

  4. [BZOJ2791]:[Poi2012]Rendezvous(塔尖+倍增LCA)

    题目传送门 题目描述 给定一个有n个顶点的有向图,每个顶点有且仅有一条出边.每次询问给出两个顶点${a}_{i}$和${b}_{i}$​​,求满足以下条件的${x}_{i}$和${y}_{i}$:   ...

  5. POI2012题解

    POI2012题解 这次的完整的\(17\)道题哟. [BZOJ2788][Poi2012]Festival 很显然可以差分约束建图.这里问的是变量最多有多少种不同的取值. 我们知道,在同一个强连通分 ...

  6. Solution -「基环树」做题记录

    写的大多只是思路,比较简单的细节和证明过程就不放了,有需者自取. 基环树简介 简单说一说基环树吧.由名字扩展可得这是一类以环为基础的树(当然显然它不是树. 通常的表现形式是一棵树再加一条非树边,把图画 ...

随机推荐

  1. DB天气app冲刺二阶段第七天

    又冲刺了一个礼拜了 今天收获应该算是不小了 虽然进度上来说还是一点也没前进 但是找到了好几个突破口 明天继续 今天先不多说了困了..

  2. c语言关于二进制的输出

    c语言中的二进制输出是没有占位符的,不像八进制:%o: 和十六进制:x%: c中二进制的输出 //右移31位,从最高为开始和1做&运算,得到每一位的二进制数值 void printbinry( ...

  3. 编译时和运行时、OC中对象的动态编译机制

    编译时 编译时顾名思义就是正在编译的时候.那啥叫编译呢?就是编译器帮你把源代码翻译成机器能识别的代码.(当然只是一般意义上这么说,实际上可能只是翻译成某个中间状态的语言.比如Java只有JVM识别的字 ...

  4. Android keyevent 中的各个值

    Android keyevent 中的各个值,在使用adb shell input 的时候用得到. 是从http://blog.csdn.net/huiguixian/article/details/ ...

  5. Useful related java API for Android

    Language_suport and Other Language-Oriented API: strings,exceptions, threads, #java.lang.* offers th ...

  6. [设计模式] 9 装饰者模式 Decorator

    转:http://www.jellythink.com/archives/171#prettyPhoto 什么是装饰模式? 在GOF的<设计模式:可复用面向对象软件的基础>一书中对装饰模式 ...

  7. PHP输出中文乱码的问题

    用echo输出的中文显示成乱码, 其实应该是各种服务器脚本都会遇到这个问题, 根本还是编码问题, 一般来说出于编码兼容考虑大多的页面都将页面字符集定义为utf-8 <meta http-equi ...

  8. [转载]Spring Bean Configuration Inheritance

    转自: http://www.mkyong.com/spring/spring-bean-configuration-inheritance/ In Spring, the inheritance i ...

  9. Tries

    trie,又称前缀树或字典樹,是一种有序树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的所有子孙都有相同的前缀,也就是这个节 ...

  10. Xamarin for Visual Studio 3.11.658 Alpha 版 破解补丁

    注意:此版本为 Alpha 版,版本迭代较频繁,仅供尝鲜 前提概要 全新安装请参考 安装 Xamarin for Visual Studio. 最新稳定版请参考 Xamarin for Visual ...