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

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

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

这题还是比较简单的,把环作为根然后类似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. ./configure --prefix=

    一直用这个选项prefix=,但不知道,啥意思. 转载自:----------------------------------------------------------------------- ...

  2. Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp

    题目链接: 题目 A. Reberland Linguistics time limit per test:1 second memory limit per test:256 megabytes 问 ...

  3. linux进程管理之开机启动

    下面用自启动apache为例;自启动脚本:/usr/local/apache2/bin:./apachectl start文件位于/etc/rc.d/init.d下,名为apached, 注意要可执行 ...

  4. Eclipse 中Alt+/快捷键失效的解决办法。

    1.Eclipse下进入Windows ->Preperences ->General ->keys2.把word completion的快捷键设置alt+/删掉! 3.把Conte ...

  5. xml存储图片 二进制存储图片

    一.保存图片到XML文件 /// <summary> /// 保存图片到XML文件 /// </summary> private void UploadImageToXml() ...

  6. tornado解析http body的过程分析

    tornado解析http body的过程分析 在最近写的一个RESTful API Server过程中,发现tornaod对解析POST BODY的内容有限制. 而在以前用web.py则没有这个限制 ...

  7. POJ 1486 Sorting Slides(寻找必须边)

    题意:找出幻灯片与编号唯一对应的情况 思路: 1:求最大匹配,若小于n,则答案为none,否则转2 (不过我代码没有事先判断一开始的最大匹配数是否<n,但这样也过了,估计给的数据最大匹配数一定为 ...

  8. CKeditor 配置使用

    CKeditor 配置使用 一.使用方法:1.在页面<head>中引入ckeditor核心文件ckeditor.js<script type="text/javascrip ...

  9. Hibernate逍遥游记-第12章 映射值类型集合-001映射set(<element>)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  10. PostMessage与SendMessage各自的问题

     深入解析SendMessage.PostMessage 本文将使用C++语言,在MFC框架的配合下给出PostMessage.SendMessage等的使用方式与使用不当造成的后果(讨论均针对自定义 ...