A brief introduction to the multiplication algorithm of LCA
For all nodes on the tree, it is easy to find the direct parent node and its depth through DFS
We can deal with the first 2^i fathers of each node by a principle like RMQ.
This process can be built either as a double loop after Dfs or as in a tree profile template.
Personal recommendation The latter, there will be fewer unnecessary operations, but because of the advantages of the log algorithm makes them very different
U,v for the two nodes given in the topic, the first step we have to make is to adjust the u,v to the same depth
The approach is simple, just use 2^i from big to small to approximate the answer
After adjusting to the same depth two nodes go forward together, as in the case of adjusting the depth above
Details:
When the depth of the u,v is just the same at the beginning, it must be a special sentence, because arithmetic to ln (0) will make an error
BZOJ1602 Simple LCA Template questions
BZOJ1787 The main topic is to let us find the tree three points to the same point of the Benquanga and the smallest, the output of the point and the minimum edge right
As we can see from the above example, the LCA that is not three points is the right edge and the smallest
Because the red edge on the graph will walk two times when it goes to the red node, and walk to the green node only once, and the nodes on the other paths once
Found in fact with the center of the tree is a bit of a relationship ... Then I thought of the violent Zjoi day1t1
In fact, this problem is not so troublesome ... Because there are only three points, it's easy to think that the final answer must be a two-point LCA.
Then enumerate three times on it, first of which two points to do an LCA, to find out the path edge right and, then the new points and the remaining points to do LCA, to find the path and
The answer to the first question is the point that was found after the first LCA.
BZOJ2144
It's almost impossible to see what the problem is with LCA ...
But when I think about it, I think this idea is simply too good ...
For a state we are represented by triples (x, y, z)
One kind of transfer is jumping from the outside point to the middle, and obviously because of the "only one piece" limit in the title, there is only one way
And there are two ways to jump from the middle to the sides.
According to the previous idea, here directly BFs knocked up.
We're thinking about every ternary group jumping to the middle, there must be a final state.
And this final state jumps outward to produce a series of states like binary trees.
We'll define the outer jump as a connecting edge to the son state, and the upward bar is defined as the connecting edge to the parent node
The length of the path on the tree of two states is exactly what is required in the topic
The first question is simply to determine whether the state of the root node is the same.
But for 10^9 's data, it's obviously not enough to think about it.
We are faced with two problems, one is depth how to ask (the array does not open the State is also enumerated), the second is the first 2^i father how to ask
We found these two issues to be linked.
Consider a state (x, Y, z), set t1=y-x,t2=z-y
When T1>t2, it is clear that the right Z jumps to the left, but can you jump a few steps? We can solve the inequalities to conclude: (t1-1) Div T2 Step
When T2>T1 is the same
Every time we update t1,t2 we find that it is actually a process of dividing, that is, not a few steps to reach the root node
You can also add depth to this process
It is also possible to calculate the state of the 2^i father of a known state based on this process.
That way, the problem is almost solved.
The last detail, read in the state is unordered, to sort and then do/w\
Algorithm consolidation exercises for LCA on [bzoj1602&bzoj1787&bzoj2144] Trees