LCA(Tarjan)
program LCA(Tarjan);
type arr=record
u,v,w,next:longint;
end;
const maxn=;
maxm=;
var eg:array[..maxm*] of arr;
last,lasq,ans,fa:array[..maxn] of longint;
flag:array[..maxn] of boolean;
n,i,j,q,x,y:longint;
procedure add(u,v:longint);
begin
inc(j);
eg[j].u:=u;
eg[j].v:=v;
eg[j].next:=last[u];
last[u]:=j;
end;
procedure adt(u,v,w:longint);
begin
inc(j);
eg[j].u:=u;
eg[j].v:=v;
eg[j].w:=w;
eg[j].next:=last[u];
lasq[u]:=j;
end;
function find(x:longint):longint;
begin
if fa[x]=x then exit(x);
fa[x]:=find(fa[x]);
exit(fa[x]);
end;
procedure lca(u:longint);
var v,i:longint;
begin
i:=last[u];
while i<>- do
begin
v:=eg[i].v;
if not flag[v] then
begin
flag[v]:=true;
lca(v); fa[v]:=u;
end;
i:=eg[i].next;
end;
i:=lasq[u];
while i<>- do
begin
v:=eg[i].v;
if flag[v] and (ans[eg[i].w]=-) then
ans[eg[i].w]:=find(v);
i:=eg[i].next;
end;
end;
begin
for i:= to maxn do begin last[i]:=-; lasq[i]:=-; end;
j:=-;
readln(n,q);
for i:= to n- do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
for i:= to q do
begin
readln(x,y);
adt(x,y,i);
adt(y,x,i);
ans[i]:=-;
end;
fillchar(flag,sizeof(flag),false);
for i:= to n do fa[i]:=i;
flag[]:=true;
lca();
for i:= to q do writeln(ans[i]);
end.
LCA(Tarjan)的更多相关文章
- LCA (Tarjan&倍增)
LCA_Tarjan 参考博客:https://www.cnblogs.com/JVxie/p/4854719.html LCA的Tarjan写法需要结合并查集 从叶子节点往上并 int Find ( ...
- LCA(tarjan)
这是LCA算法中的一种,Tarjan算法 其实这么说也有点不对,应该是Tarjan+DFS进行解决 LCA又称为最近公共祖先 那么什么是最近公共祖先: 在一棵没有环的树上,每个节点肯定有其父亲节点和祖 ...
- POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】
任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total ...
- LCA(Tarjan算法)模板
一.查询一组的LCA Nearest Common Ancestors A rooted tree is a well-known data structure in computer science ...
- H - Rescue the Princess ZOJ - 4097 (tarjan缩点+倍增lca)
题目链接: H - Rescue the Princess ZOJ - 4097 学习链接: zoj4097 Rescue the Princess无向图缩点有重边+lca - lhc..._博客园 ...
- LCA(最近公共祖先)--tarjan离线算法 hdu 2586
HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- 最近公共祖先(LCA)---tarjan算法
LCA(最近公共祖先).....可惜我只会用tarjan去做 真心感觉tarjan算法要比倍增算法要好理解的多,可能是我脑子笨吧略略略 最近公共祖先概念:在一棵无环的树上寻找两个点在这棵树上深度最大的 ...
- Tarjan算法离线 求 LCA(最近公共祖先)
本文是网络资料整理或部分转载或部分原创,参考文章如下: https://www.cnblogs.com/JVxie/p/4854719.html http://blog.csdn.net/ywcpig ...
- POJ 1236 Network of Schools(tarjan算法 + LCA)
这个题目网上有很多答案,代码也很像,不排除我的.大家的思路应该都是taijan求出割边,然后找两个点的LCA(最近公共祖先),这两个点和LCA以及其他点构成了一个环,我们判断这个环上的割边有几条,我们 ...
随机推荐
- Objective-C:Foundation框架-常用类-NSValue
NSNumber是NSValue的子类,前者只能包装数字,后者可以包装任意值.NSArray.NSDictionary只能存储OC对象,不能存储结构体.因此,如果想要在NSArray.NSDictio ...
- CSS 收集
margin:0 auto margin后面如果只有两个参数的话,第一个表示top和bottom,第二个表示left和right, 因为0 auto,表示上下边界为0,左右则根据宽度自适应相同值(即居 ...
- spring+springmvc+mybatis整合
1.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...
- HDU 3255 扫描线(立方体体积并变形)
Farming Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- PictureWebHandler
using System; using System.Configuration; using System.Drawing; using System.Drawing.Imaging; using ...
- batch insert 1 million datas into mysql
最近尝试插入1百万条数据进db,以mysql为例. 1. 顺序insert 先写了个无脑的for循环作为base-line,插1万条耗时1m53s,根本不敢插1百万. foreach(var stud ...
- Program A-归并排序
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- 类似github的框架
github是程序员经常上的网站,但如果是在一家苦逼不能访问外网的公司,那不能把自己的代码托管在github上绝对是一件非常痛苦的事情.如果想要在公司内网也可以用github托管自己的代码,那就要自己 ...
- 在shell脚本中进行条件控制以及使用循环
转载请标明:http://www.cnblogs.com/winifred-tang94/ if条件语句语法: if [ 条件表达式 ] then 代码 else 代码 fi 注意:在上面的if条件语 ...
- iOS多语言备选机制
近期伊书突然接到一些外国用户的投诉,说伊书界面变成了中文,但他们系统语言不是中文,是法文俄文日文等,伊书只支持中文跟英文,在不支持系统所用语言的时候,理应会自动选用英文,不知为什么会选成了中文,经过艰 ...