bzoj2791
每个顶点有且仅有一条出边是什么意思呢
类似一棵树,树上的边都是由儿子指向父亲的,并且这个东西带着一个环
也就是一个个有向环套有向树……
这题还是比较简单的,把环作为根然后类似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的更多相关文章
- 【BZOJ2791】[Poi2012]Rendezvous 倍增
[BZOJ2791][Poi2012]Rendezvous Description 给定一个n个顶点的有向图,每个顶点有且仅有一条出边.对于顶点i,记它的出边为(i, a[i]).再给出q组询问,每组 ...
- [BZOJ2791][Poi2012]Rendezvous
2791: [Poi2012]Rendezvous Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 95 Solved: 71[Submit][Sta ...
- BZOJ2791 Rendezvous
Description给定一个n个顶点的有向图,每个顶点有且仅有一条出边.对于顶点i,记它的出边为(i, a[i]).再给出q组询问,每组询问由两个顶点a.b组成,要求输出满足下面条件的x.y:1. ...
- [BZOJ2791]:[Poi2012]Rendezvous(塔尖+倍增LCA)
题目传送门 题目描述 给定一个有n个顶点的有向图,每个顶点有且仅有一条出边.每次询问给出两个顶点${a}_{i}$和${b}_{i}$,求满足以下条件的${x}_{i}$和${y}_{i}$: ...
- POI2012题解
POI2012题解 这次的完整的\(17\)道题哟. [BZOJ2788][Poi2012]Festival 很显然可以差分约束建图.这里问的是变量最多有多少种不同的取值. 我们知道,在同一个强连通分 ...
- Solution -「基环树」做题记录
写的大多只是思路,比较简单的细节和证明过程就不放了,有需者自取. 基环树简介 简单说一说基环树吧.由名字扩展可得这是一类以环为基础的树(当然显然它不是树. 通常的表现形式是一棵树再加一条非树边,把图画 ...
随机推荐
- 如何利用OpenCV自带的级联分类器训练程序训练分类器
介绍 使用级联分类器工作包括两个阶段:训练和检测. 检测部分在OpenCVobjdetect 模块的文档中有介绍,在那个文档中给出了一些级联分类器的基本介绍.当前的指南描述了如何训练分类器:准备训练数 ...
- Oracle 多行记录合并/连接/聚合字符串的几种方法
怎么合并多行记录的字符串,一直是oracle新手喜欢问的SQL问题之一,关于这个问题的帖子我看过不下30个了,现在就对这个问题,进行一个总结.-什么是合并多行字符串(连接字符串)呢,例如: SQL&g ...
- Ajax ContentType 列表
".*"="application/octet-stream" ".001"="application/x-001" & ...
- 2729:[HNOI2012]排队 - BZOJ
题目描述 Description某中学有n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人 ...
- ios 图形学习笔记
一.显示文本: 1.UIFont,用于设置显示的字体 初始化方法:fontNamesForFamilyName:(类方法) fontWithName:size:(类方法) 2.绘制文本的方法: NSS ...
- 把eclipse"中文版"变成"英文版"
在Eclipse.exe当前路径下,直接新建快捷方式,右键属性,添加如下参数即可 eclipse.exe -nl en
- uva 1423 拓扑排序
刘书上例题 拓扑排序 #include <cstdio> #include <cstdlib> #include <cmath> #include <map ...
- 关于ios 8 7 下的模态窗口大小的控制 代碼+場景(mainstoryboard)( Resizing UIModalPresentationFormSheet )
1 代碼 UIViewController* modalController = [[UIViewController alloc]init];modalController.modalTransit ...
- Winform基础 -- 菜单
快速创建默认菜单 使用控件 MenuStrip : 点击菜单的右上方小三角:选择 [插入标准项] 即可显现出标准的菜单格式: 如果想添加更多的菜单项,可以在 [请在此处键入] 处输入菜单项的名称 ...
- Hadoop格式化HDFS报错java.net.UnknownHostException: localhost.localdomain: localhost.localdomain
异常描述: 在对HDFS格式化,执行hadoop namenode -format命令时,出现未知的主机名的问题,异常信息如下所示: [shirdrn@localhost bin]$ hadoop n ...