LCA problem is a kind of classical tree problem
More diverse approaches
such as violence, multiplication, and so on.
Here today to tell you about the Tarjan algorithm!
Tarjan to calculate LCA is a stable and high-speed algorithm
Time complexity can be preprocessed o (n + m), query O (1)
Its main idea is to DFS and to check the set
1. Enter the data to find the root node (or input) and survival up
2. Enter each pair of points you want to find (two points), and save them (also saved into a diagram)
3. Deep search from the root node to each of its child nodes
4. Also open an array of type BOOL to record whether this node has searched
5. When the p node is searched, mark p as already searched.
6. Then traverse all nodes connected to P and mark it as already searched.
7. Then merge the child nodes of P with P (here to use and check the set)
8. Then traverse all the child nodes of P that have a query relationship with P
9. If the child node has been traversed, the child node and the parent node of P must be merged
There may still be a lot of people who don't fully understand the algorithmic process of this text narrative.
The code is directly below (the comments are detailed)
#include <cstdio>#include<cstring>#include<cstdlib>#include<cctype>#include<cmath>#include<string>#include<iostream>#include<algorithm>#include<map>#include<Set>#include<queue>#include<stack>#include<vector>using namespaceStd;typedefLong Longll;Const intMAXN = 5e5 +5;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;}structdrug{intnext, to, LCA;} EDGE[MAXN<<1], qedge[maxn<<1];//Edge[n] is a linked list of trees; Qedge[n] is a list of two nodes that need to query the LCAintN, m, s, x, y;intNum_edge, Num_qedge, HEAD[MAXN], QHEAD[MAXN], FATHER[MAXN];BOOLVISIT[MAXN];//Judging If you've been found.voidAdd_edge (int from,intTo)//create a linked list of trees{edge[++num_edge].next = head[ from]; Edge[num_edge].to=to ; head[ from] =Num_edge;//printf ("#%d #%d #%d #%d\n", Num_edge, Head[from], from, edge[num_edge].next);}voidAdd_qedge (int from,intTo)//establish a linked list of two nodes that need to query LCA{qedge[++num_qedge].next = qhead[ from]; Qedge[num_qedge].to=to ; qhead[ from] =Num_qedge;}intFindintX//Find Father function{ if(Father[x] ^ x) father[x] =find (father[x]); returnfather[x];}voidDfsintX//The whole tree is considered as a small tree with node x as its root node, and the initial value of x is S;{father[x]= x;//since node x is considered to be the root node, the father of X is set to its ownVISIT[X] =1;//marked as having been searched for(intk = Head[x]; K K=edge[k].next)//Traverse all nodes connected to x { if(!visit[edge[k].to])//if not searched{DFS (edge[k].to);//Use this node as the root node to make a small treeFather[edge[k].to] = x;//Reset x's child node's father to x } } for(intk = Qhead[x]; K K = qedge[k].next)//search for all queries that contain node x { if(Visit[qedge[k].to])//If another node has been searched{Qedge[k].lca=find (qedge[k].to); //set the ancestor of another node to the nearest public ancestor of these two nodes if(K &1) Qedge[k +1].lca =Qedge[k].lca; //because each set of queries becomes two groups, the results of 2n-1 and 2n are the same ElseQedge[k-1].lca =Qedge[k].lca; } }}intMain () {n= Read (), M = Read (), s = Read ();//number of input nodes, number of queries and root node for(inti =1; I < n;i++) {x= Read (), y = Read ();//Enter each edgeAdd_edge (x, y); Add_edge (y, x); } for(inti =1; I <= m;i++) {x= Read (), y =read (); //enter every query, consider (U,V) when you find u but V is not found, so (u,v) (V,u) All recordsAdd_qedge (x, y); Add_qedge (y, x); } dfs (s); for(inti =1; I <= m;i++) printf ("%d\n", Qedge[i <<1].LCA);//as a result, only one set of outputs can be//printf ("%d", Num_edge); return 0;}
Tarjan to seek LCA