tarjan seeking LCA study notes

tarjan seeking LCA study notes

In fact, in essence, asking offline processing,
we can all ask this as similar to the way
(Figure supplemented later on)
every increase LCA, when just two points connectivity between two points is the highest point on LCA .
DETAILED DESCRIPTION:
Each time all subtrees dfs search, after the search within the subtree father all points marked the highest point grandparent communication, when the search query to the point, when the point has been found over the other, on the LCA it grandparent the highest point in communication.
(FIG supplemented later on)

void tarjan(int x,int fa){
    int v;
    for(int i=head[x];i;i=e[i].nxt) if(e[i].to!=fa) tarjan(e[i].to,x),f[e[i].to]=x;
    for(int i=head2[x];i;i=q[i].nxt){
        v=q[i].to;
        if(book[v]) lca[q[i].w]=getf(v);
    } 
    book[x]=1;
}
for(int i=1;i<=m;++i){
        t1=read(),t2=read(),que[i].x=t1,que[i].y=t2;
        if(t1==t2){lca[i]=t1; continue;}
        add(t1,t2,i,q,head2),add(t2,t1,i,q,head2);
    }
    tarjan(1,0);

Guess you like

Origin www.cnblogs.com/ljk123-de-bo-ke/p/11736090.html