Finding the closest common ancestor of two nodes on the tree
Algorithm 1: Tarjan
LCA (u ){
Make-set (u)
Ancestor [find-set (u)] = u // sets the ancestor of the set where u is located
V {
LCA (V)
Union (v, u) // merge the subset generated by V into U.
Ancestor [find-set (u)] = u // prevents the change of the Set root (Representative) of U by means of tree-like heuristic combination.
}
Checked [u] = true
For each (u, v) belongs to P {
If checked [v] = true
The nearest common ancestor of U and V is ancestor [find-set (v)].
}
}
# Include <cstdio> # include <iostream> # include <cstring> # include <algorithm> # include <vector> # include <queue> using namespace STD; /* offline algorithm of time complexity O (N + q), you must first record the query for each query LAC (u, v), at u, add */const int maxn = 10010 to each query queue of V; Class lca_tarjan {public: int N, Father [maxn]; bool vis [maxn]; vector <int> edge [maxn]; vector <int> Query [maxn]; void Init (int n) {for (INT I = 1; I <= N; I ++) {vis [I] = false; edge [I]. clear (); Query [I ]. Clear ();} make_set (n);} void make_set (int n) // subscript starts from 1 {for (INT I = 1; I <= N; I ++) father [I] = I;} int find (INT U) {If (u = Father [u]) return U; return father [u] = find (father [u]);} void Union (int u, int v) // {int Fu = find (u) can be optimized ); int FV = find (V); If (FV! = Fu) Father [FV] = Fu;} void Tarjan (int u) {FATHER [u] = u; int SZ = edge [u]. size (); For (INT I = 0; I <SZ; I ++) {Tarjan (edge [u] [I]); Union (u, edge [u] [I]); father [find (u)] = u;} vis [u] = true; SZ = Query [u]. size (); For (INT I = 0; I <SZ; I ++) {If (vis [Query [u] [I] = true) cout <father [find (Query [u] [I])] <Endl ;}}; int main () {lca_tarjan t; int T; scanf ("% d", & T); While (t --) {scanf ("% d", & T. n); T. init (T. n); int U, V; bool notroot [maxn] = {0}; For (INT I = 1; I <t. n; I ++) {scanf ("% d", & U, & V); T. edge [u]. push_back (V); notroot [v] = true;} scanf ("% d", & U, & V); T. query [u]. push_back (V); T. query [v]. push_back (U); For (INT I = 1; I <= T. n; I ++) if (notroot [I] = false) returns t.tar Jan (I); break ;}} return 0 ;}
Conversion from algorithm 2 LCA to rmq:
Perform DFS on the root tree T and write down the traversal nodes in order. We will get a sequence with a length of 2n-1, every node of the Euler's sequence F called T appears in the Euler's sequence. We record that the first occurrence of node u In the Euler's sequence is pos (u)
Based on the nature of DFS, for the process of traversing the two nodes U and V from pos (u) To pos (V), there is only one time after the LCA (u, v, and the depth is the depth sequence B [pos (u )... The smallest of pos (v)] is LCA (t, u, v) = rmq (B, pos (u), pos (V )), the problem scale is still O (n), which proves that the problem can be converted to the problem of rmq) completed in time!
/* 1. time complexity O (N * logn + q) 2. online algorithm 3. after the search, the N nodes get 2 * n-1 numbers */# include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <queue> # include <vector> using namespace STD; const int maxn = 10010; const int pp = 25; Class lca_rmq {public: int N; int pow2 [PP]; // 2 ^ K int Tim; // time stamp, int first [maxn] starting from 1; // The timestamp int nodeid [maxn * 2] of the first access to the node; // node number corresponding to the corresponding time stamp int Dep [maxn * 2]; // depth int DP [maxn * 2] [PP]; // after searching, get 2 * n-1 numbers bool vis [maxn]; // record whether the node has been accessed by vector <int> edge [maxn]; void Init (int n) {for (INT I = 0; I <pp; I ++) pow2 [I] = (1 <I); For (INT I = 1; I <= N; I ++) {edge [I]. clear (); vis [I] = false ;}} void addedge (INT U, int v) {edge [u]. push_back (V);} void DFS (int u, int d) // U indicates the node, and D indicates the depth {Tim ++; // timestamp + 1 vis [u] = true; nodeid [Tim] = u; // record the node first [u] = Tim corresponding to the timestamp; // record the timestamp Dep [Tim] = D for the first access of node u; // The depth of the root node is 1 int SZ = edge [u]. size (); For (INT I = 0; I <SZ; I ++) {int v = edge [u] [I]; if (vis [v] = false) {DFS (v, D + 1); Tim ++; nodeid [Tim] = u; // If the accessed node is not a leaf node, add Dep [Tim] = D ;}} void ST (INT Len) {int K = (INT) (log (LEN + 1.0) /log (2.0); For (INT I = 1; I <= Len; I ++) DP [I] [0] = I; for (Int J = 1; j <= K; j ++) for (INT I = 1; I + pow2 [J]-1 <= Len; I ++) {int A = DP [I] [J-1]; // A, B are subscript int B = DP [I + pow2 [J-1] [J-1]; if (DEP [a] <Dep [B]) DP [I] [J] = A; else DP [I] [J] = B ;}} int rmq (int x, int y) {int K = (INT) (log (Y-x + 1.0)/log (2.0 )); int A = DP [x] [k]; int B = DP [y-pow2 [k] + 1] [k]; If (DEP [a] <Dep [B]) return A; else return B;} int LCA (INT U, int v) {int x = first [u]; int y = first [v]; If (x> Y) swap (x, y); int Index = rmq (x, y); Return nodeid [Index] ;}; lca_rmq t; int main () {int T; scanf ("% d", & T); While (t --) {int U, V, RT; bool notroot [maxn] = {0 }; scanf ("% d", & T. n); T. init (T. n); For (INT I = 1; I <t. n; I ++) {scanf ("% d", & U, & V); T. addedge (u, v); notroot [v] = true;} scanf ("% d", & U, & V); For (INT I = 1; I <= T. n; I ++) if (notroot [I] = false) {RT = I; break;} t. tim = 0; T. DFS (RT, 1); // The root node depth is 1 TB. ST (2 * t. n-1); int ans = T. LCA (u, v); printf ("% d \ n", ANS);} return 0 ;}