Algorithm 将LCA解从树扩展到DAGs

Algorithm 将LCA解从树扩展到DAGs,algorithm,tree,directed-acyclic-graphs,rmq,lowest-common-ancestor,Algorithm,Tree,Directed Acyclic Graphs,Rmq,Lowest Common Ancestor,我使用的是使用RMQ在树中求解LCA的算法 它基本上是这样工作的: 我们在树上执行Euler漫游,得到3个数组 E = {0,1,0,2,0} //the vertexes in the order they were visited L = {0,1,0,1,0} //the levels of each of these vertexes I = {0,1,3} //index of the first ocurrence of each vertex 如果我们想要LCA(u

我使用的是使用RMQ在树中求解LCA的算法

它基本上是这样工作的:

  • 我们在树上执行Euler漫游,得到3个数组

    E = {0,1,0,2,0} //the vertexes in the order they were visited
    L = {0,1,0,1,0} //the levels of each of these vertexes
    I = {0,1,3}     //index of the first ocurrence of each vertex
    
  • 如果我们想要
    LCA(u,v)
    ,我们只需要从
    I[u]
    I[v]
    得到
    L
    的RMQ

示例:LCA(1,2)=从索引
I[1]=1到
I[2]=3的L的RMQ

L[1:3]=[1,0,1],RMQ[1,0,1]=0,这是指数2,因此LCA(1,2)=E[2]=0


我的问题是:如何扩展此解决方案以适合有向无环图

就这样,它不起作用。假设我们有这个DAG:

如果我们计算
E
L
I
,我们将得到:

E = {0,1,3,1,4,1,0,2,4,2,5,2,0}
L = {0,1,2,1,2,1,0,1,2,1,2,1,0}
I = {0,1,7,2,4,10}
通过计算LCA(2,4)可以看出它是错误的,这显然应该是2,因为2是4的父项,但按照算法,我们将计算:

RMQ(I[2]:I[4])=RMQ(7,4)=RMQ(4,7)=RMQ({2,1,0,1})=0

0有索引6,因此LCA(2,4)=E[6]=0,这是错误的

有没有办法让它发挥作用