Tarjan to seek LCA

Source: Internet
Author: User

Tarjan The LCA is an off-line LCA, it needs to know all the queries, and then through the DFS and check set maintenance for the LCA, this is a linear approach, if there are N nodes m asked, then the complexity of O (n+m).

How do you do it in concrete ways? In fact, it is the dfs+ and check set.

We first use the chain forward to the star to save the tree, the existence of all the inquiry relationship (why the next one), pay attention to the question of the relationship to save the graph. The parent node of each node is initially set to itself. Then we start Dfs from the root node, first to Dfs's own son, and when Dfs goes to a node with no sons, we begin to traverse all queries related to this node. (To know why the chain forward to the star, because this can be linear run, otherwise it is m^2 complexity) if that node has been visited, then the two points of the LCA is that node along and look up the set of Father. If you do not have access to direct disregard can.

Then, when the node returns, the father of each node is set to a point above itself, and the point is set to be accessed.

And then like all offline ... We need to record the number of each LCA, and the final output will be output sequentially.

The reason why the Tarjan algorithm can be linear is that he takes advantage of the key that only needs M-times to ask. (Of course, not only these, people multiply more than one log), in general, the complexity of Tarjan is the best, but if the M is far greater than the case of N, or use RMQ to find the best LCA. The multiplication of words seems to be more suitable for the time when preprocessing is more troublesome.

As for the tree profile ... General tree profiling for LCA is actually a handy thing ... It seems that there is very little to write a tree profile for LCA ...

Take a look at the code of Tarjan for LCA.

#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>#include<cstring>#defineRep (i,a,n) for (int i = a;i <= n;i++)#definePer (i,n,a) for (int i = n;i >= a;i--)#defineEnter Putchar (' \ n ')#definePR pair<int,int>#defineMP Make_pair#defineFi first#defineSC Secondusing namespaceStd;typedefLong Longll;Const intM =500005;Const intN =10000005; intRead () {intAns =0, op =1; CharCH =GetChar ();  while(Ch <'0'|| CH >'9')    {    if(ch = ='-') op =-1; CH=GetChar (); }     while(Ch >='0'&& CH <='9') {ans*=Ten; Ans+ = CH-'0'; CH=GetChar (); }    returnAns *op;}structedge{intNext,to,id;} E[m<<2],q[m<<2];intN,m,s,x,y,head[m],qhead[m],ecnt,qcnt,fa[m],lca[m];BOOLVis[m];intGETFA (intx) {    returnFA[X] = = x? X:FA[X] =GETFA (fa[x]);}voidAddintXinty) {e[++ecnt].to =y; E[ecnt].next=Head[x]; HEAD[X]=ecnt;}voidADDQ (intXintYintg) {q[++qcnt].to =y; Q[qcnt].next=Qhead[x]; Q[qcnt].id=G; QHEAD[X]=qcnt;}voidTarjan (intXintf) {     for(inti = Head[x];i;i =E[i].next) {    if(e[i].to = = F | | vis[e[i].to])Continue;    Tarjan (E[I].TO,X); Fa[e[i].to]=x; }     for(inti = Qhead[x];i;i =Q[i].next) {    if(!vis[q[i].to])Continue; Lca[q[i].id]=GETFA (q[i].to); } Vis[x]=1;}intMain () {n= Read (), M = Read (), s =read (); Rep (I,1, N-1) x = Read (), y = Read (), add (x, y), add (y,x), fa[i] =i; Fa[n]=N; Rep (I,1, m) x = Read (), y =read (), ADDQ (X,y,i), ADDQ (y,x,i);    Tarjan (s,s); Rep (I,1, N) printf ("%d\n", Lca[i]); return 0;}

Tarjan to seek LCA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.