This time, we're going to learn something that sounds pretty empty.
Yes, it's more imaginary.
After all, it's all on the empty stuff ...
Virtual tree, as the name implies, is an unreal tree "fog"
It can save all the information of the whole tree for a part of the point, and ignore the part of the point, which can increase the efficiency of dp/Point division.
In order to give you a better example (lest you don't understand what a virtual tree can do), let's start with a topic: the War of attrition
It is easy to see that this problem requires a tree DP for each query, and then each time in the original tree DP has $o\left (n\right) $ time overhead, M-Times asked to finish sure Tle
So how to solve it? Maybe some people will say offline blind, but in case this problem is forced in floss???
We're going to use the data structure of the virtual tree.
Considering a set of queries, we have some points in the tree that are "ask points", and the remaining points are not
In other words, we just need to process the information on these "ask points" and then merge the information from the enquiry point.
Eh, wait, what if it's not all a parent-child relationship between the asking points?
It looks like we're going to add the LCA to the inquiry and put it in the tree.
In this way, our new tree has all the inquiry points for this set of queries, and the LCA between them, and then the order of magnitude of this point is $o\left (the number of points in question \right) $ level other
We see, in the scope of the topic data, that the sum of all the query points does not exceed 300000.
Ok! So the DP will not time out!!
So above this step, we found the idea of doing the problem: to build the virtual tree, in the virtual tree DP
So, how do you implement the process of building a virtual tree?
This seems like a bit of a hassle, because there's no way we can find an additional LCA for a point set, so we have to do it in a different angle.
The idea here is more complicated, it is not worth to say, so we can provide the method directly.
We maintain a monotonous stack, the elements of this stack are about the depth of monotony, that is to say, we keep a chain of trees starting from the current root.
Start by ordering all the "ask points" in the DFS sequence, then adding them to the stack in turn
Each time, we set the grand as the current LCA for the stack node and the top node of the current stack
Then do a loop like this:
If the current stack top depth is greater than grand, we will connect a virtual tree edge between the top of the stack and the second element of the stack, and bounce the stack top off until the depth of the top of the stack is less than or equal to the grand
At this point, the grand and the previous ejected element are connected to the edge
Then add the grand to the top of the stack, and then add in the Stack node
All the "Ask points" after the stack, and then put the elements of the stack one after another to bounce off and the edge is good
Note that in this process, all edges are only carried out during the stack, so be careful not to write wrong
This is built out of the virtual tree is less than the number of points, and in the DFS sequence we can also maintain the depth and subtree size and other information, behind the virtual tree can still be used
However, sometimes (such as the above example), a node must not be a point of inquiry, at this point, you can first put this node (such as an example of a number) into the stack, as the root of the virtual tree
Otherwise, we need to determine whether the stack is empty at each new query point, and if it is empty, put the node directly into the stack.
In this way, the last remaining element in the stack is the root of the virtual tree.
Of course, some of the topics because of the need for some and DFS when the root node identified (such as number 1th) about the information, so must be a number as the root, this time will be in the DP inside to judge
Said so much, in fact, the construction of virtual tree is mostly related to the topic, so still want to read more questions
Here are three examples of virtual trees, respectively, corresponding to the above paragraph three of the situation
SDOI2011 consumption War
HEOI2014 Big Project
HNOI2014 World Tree
Virtual tree can be seen as a means of optimizing DP time complexity, the real essence of the topic is still on DP
Like the World tree this problem on DP very disgusting, write up thief uncomfortable ...
To sum up, the virtual tree is an optimization tree structure of the data structure "good around Ah", the general combination of DP or point division of the use of (as if relatively rare points of treatment)
Of course, there may be some combination of tree Mo team AH heuristic algorithm ah kind of disgusting problems, but not much.
In fact, the virtual tree algorithm stems from the idea of removing redundant information, which integrates many, unwanted information on the edge of a virtual tree.
This is particularly evident in the world tree problem.
Save algorithm Learning-data structure-virtual tree