Objective
Solve multiple queries about certain points on the tree, and the complexity is about the number of points per query.
Ideas
The point of each inquiry and certain LCA (i.e. key points) are condensed to the virtual tree, and the connecting edge between two points contains the information of the path between two points in the original tree, and then the violence on the virtual tree (?). Processing
Practice
Sort by DFS sequence first, so that the points in its subtree are done when the points are not in a tree of ideas
Use a stack to record the chain from the virtual tree root to the current point. Although the root is a person who can do, but for convenience directly to 1, and put 1 and the first point pressed into the stack
Now add a point P, set the top element of the stack is x,p and the nearest public ancestor of X is the LCA
There are two cases (LCA is definitely not equal to P, because it is done in DFS order):
1.lca=x, press p directly into the stack.
2.lca!=x
This means that the LCA is above X, and the subtree of X is done, we need to find a suitable position in the stack to put the LCA down, put the X into a proper spot, kick the x out, and add the P in.
Loop to do, set the top of the stack below the element is Y, if:
1.DFN[Y]>DFN[LCA], i.e. LCA on y, that gives y and x a side, then kicks X to continue doing
2.DFN[Y]=DFN[LCA], then LCA is already in the virtual tree, give y and x side, kick X and break on the line
3.DFN[Y]<DFN[LCA], that is, the LCA under Y, that will give Y,x edge, kicked X after the LCA pressure, and then break
Finally, the p is also pressed into the stack. Note that the path information is also recorded on the edge when you are connected.
And then build the virtual tree on the mess on it. Note that because of the number of inquiries, absolutely not memset, need to empty something when how to add in how to reduce the back good. Clear the empty tree side of the word can Dfs
After the example, we will fill
[Template] Virtual tree