[Cactus chart] Cactus DP learning notes, cactusdp

Source: Internet
Author: User

[Cactus chart] Cactus DP learning notes, cactusdp

We start with an example to consider the general law of DP on the cactus.

Ex 1. Single-source shortest circuit problem on cactus
The shortest path on the Lenovo tree is the most short-circuited. Because of the uniqueness of the path, the O (n) search can be performed directly, but the cactus obviously does not have the uniqueness of the path.
So do we need to use the shortest path algorithm like a general undirected connected graph?
Actually, this is not required.
First, DFS processes the structure of the cactus.
Then we start DP from the starting point. If the current DP to the node is x, then enumerate every son of x. if this son node is a normal node, we can directly get the distance from the start point to this node. if the child node is a ring, the distance from v to the start point is obtained for each node v in the enumeration ring, and then from v to DP.
It is also an O (n) DP. It is superior to the brainless short circuit, so I don't know where to go!

Ex 2. Find the diameter of the cactus
We will deal with the situation with edge and unlimited power. ("edge right" is regarded as "1)
Consider the tree first. for the diameter of the tree, we can use the tree-like DP to find the depth of each node and the maximum depth of the subtree with its root, then the maximum depth + times of the different sons of each node is the answer.
Then, the cactus is similar.
First, the DFS processes the structure of the cactus.
First, the concept of a sub-cactus is proposed. The sub-Cactus x is the connected block where the edge x is located after deleting all simple paths from node x to the root.
After processing the cactus structure, we can process the maximum depth of the Child cactus for each node.
For a node, you can still use the maximum depth + large depth of different sons to update the answer.
For a ring, you can use the ringEach pairThe maximum depth of the node and the shortest length between nodes are used to update the answer.
There are two solutions:
1. if a node in the enumeration ring exists, if the shortest path between a node and the current node is clockwise, the two nodes must correspond to a range in the ring. it is not difficult to find that if the node We Are enumerating moves clockwise, the end point of this range will also move clockwise. to solve the RMQ problem where both ends move monotonically, you can use a monotonous queue for maintenance.
2. starting from the father of the ring, we split the ring into two parts based on the closer side. we can find that the shortest path of the two internal points must be inside. therefore, for each node, the shortest path in the other part must follow a prefix clockwise, and the shortest path must follow a suffix counterclockwise. You can directly process the prefix max and suffix max.

Based on the two examples, we can see that in addition to the DFS processing of the cactus structure, the non-cycle DP on the cactus is basically the same as the tree DP, when processing the cactus DP, what is more than the tree DP is how to deal with the points on the ring.
Usually the distance is related to the DP on the cactus, so we can get inspiration from the above two examples to promote more questions.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.