1. Preface
The recent public ancestor (Least Common ancestors), referred to as LCA, is a common ancestor problem raised by Professor Tarjan (yes and he) in a root tree to find the nearest two nodes U and v.
2. What is the recent public ancestor?
In a tree, each node has his father and ancestors, and the recent public ancestor is the two nodes in this tree the depth of the largest public ancestor node .
In other words, it is the nearest common ancestor node of two points on this tree. The combination and text should be a good interpretation of the recent public ancestor:
PS: In LCA, the node itself can also be regarded as its own ancestor
In this tree with node 1 root, the nearest public ancestor of 4 and 5 is the nearest public ancestor of 2,4 and 3 is the nearest public ancestor of 1,4 and 2 is the nearest public ancestor of 1,4 and 1 is 1.
3. What is offline/online algorithm
For the LCA problem, common methods are Tarjan (dfs+ and set) algorithm, multiplication algorithm, ST (dfs+st table) algorithm, the latter two are online algorithms, and the former is offline algorithm (the subsequent LCA algorithm will be updated according to the order given now).
So what is the In/offline algorithm? Here quoted a section of the Baidu Encyclopedia saying:
Online algorithm:"online algorithm means that it can be serialized in a way of processing input, that is, at the beginning do not need to know all the input." "
Offline algorithm:"refers to the basic assumptions that are based on the known input data before the algorithm is executed, that is, for an offline algorithm, you need to know all the input data of the problem at the beginning, and output the result immediately after solving a problem." "
Here for example, the online algorithm is what we often say "input a set of data, output a set of data", while the offline algorithm is "input all data in the output answer."
4.LCA Problem Method Introduction:
1.Tarjan algorithm:
2. Multiplication algorithm:
3.ST Table +dfs algorithm:
"C + +" recent public ancestor LCA (pre-knowledge)