We start with an example to consider the general law of DP on the Cactus BA.
Ex 1. Single Source shortest circuit problem on cactus
The shortest path on the Lenovo tree, because the uniqueness of the paths can be directly done by the O (n) search. But the cactus obviously does not have the uniqueness of the path of this nature.
So do we need to use the shortest path algorithm as we would for a generic undirected graph?
I don't really need it.
First, DFS deals with the structure of the cactus.
Then we start with the DP, assuming the current DP to the node is X, then enumerate every son of X. If the son node is a normal node, then we can directly get the distance from the starting point to this node. If the son node is a ring, enumerate each node V on the ring to find the V-to-start distance, The DP is then started from v.
The same is an O (n) DP. It is superior to not knowing where to go without a brain-running shortest circuit!
Ex 2. Finding the diameter of a cactus
We will deal with the situation with the right of Edge and the right of infinity. (Boundless power as a side right of 1)
The tree is still considered first. For the diameter of the tree, we can use the tree DP to find the depth of each node and the maximum depth of the subtree to which it is rooted, and then the maximum depth of each node with a different son and the greater depth of the second is the answer.
So the cactus is similar.
The first is a DFS that handles the structure of the cactus.
First, the concept of a sub-cactus, sub-cactus x is to delete the node x to the root of all the simple path on the edge after X is located in the connected block.
After processing the cactus structure, we treat each node with the maximum depth of its sub-cactus.
For a node, you can still update the answer with a different son's maximum depth + minor depth.
For a ring, you can update the answer with the maximum depth of each pair of nodes on the ring plus the shortest path length between the nodes.
At this point we have two ways to solve:
1. Enumerate a node on the ring, if there is a node and the current node is the shortest path from the current node clockwise, the two nodes must correspond to an interval on the ring. And it's not hard to see if the node we're enumerating now moves clockwise, The end of this interval will also move clockwise. The problem of RMQ that both endpoints are monotonic can be maintained with a monotone queue.
2. We start with the father of the ring, which is closer to the next, and divides the ring into two parts. It can be found that These two parts of the internal point pair of the shortest path must be internal. So for each node, it is in the other part of the quickest way is a prefix, the shortest way is to go counterclockwise is a suffix, directly processing the prefix max and suffix max can be.
According to these two examples we can see that in addition to DFS processing cactus structure is certain, the cactus on the non-ring point of DP is basically the same as the tree DP, in the processing of the cactus DP than the tree DP is how to deal with the point on the ring.
Usually the DP on the cactus is related to the distance, so we can get inspiration from the above two examples to extend to more questions.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Cactus Cactus Map" Cactus DP Learning Note