P2944 [USACO09MAR] Earthquake Damage 2 G Solution
The content below was generated entirely by machine translation. Please verify its accuracy. If anything is unclear, consult the Chinese source version.
I saw that the existing solutions did not use an STL vector, so I came to submit one.
1: Reformulating the Problem
There are nodes and undirected edges in a graph, and there are nodes that cannot be deleted. Find the minimum number of nodes that need to be deleted so that none of these fixed points can reach node 1.
2: Analysis and Modeling
After reformulating the problem, we see that we need to delete some nodes (as few as possible) to make the entire graph become two disconnected parts. The minimum-cut (maximum-flow) algorithm in network-flow algorithms can handle this problem.
【Students unfamiliar with maximum-flow algorithms can first solve this template problem】
Maximum-flow template
However, ordinary minimum cut handles “deleting some edges of a graph so that its two parts become disconnected,” while this problem asks us to delete some nodes. Therefore, we need to convert nodes into edges.
I use the method of splitting every node into two nodes (an outgoing node and an incoming node). For a specific implementation, refer to the solution for P1345 Telecowmunication.
Here is a brief explanation. First, split every point in the graph into two points: an outgoing point and an incoming point.
There is a directed edge connecting these two nodes:

Every directed edge pointing to this point can only connect to the point’s incoming node. Every directed edge starting from this point can only start at its outgoing node.

What is the use of splitting every node into an outgoing point and an incoming point? In an ordinary minimum-cut problem, if we want to know how many edges must be removed to disconnect the sink and source of a graph, we can set the weight of every edge to , paying a cost of to delete that edge.
In a minimum-cut problem based on cut vertices, we can set the weight of the edge connecting each node’s outgoing and incoming points to . Then, if we want to delete this node, we can pay a cost of to cut this edge, which deletes the node as well.
This raises a problem: the statement explicitly says that some nodes cannot be deleted. If all weights are set to , how do we handle nodes that cannot be deleted?
For these key nodes (the undeletable nodes), we can set the capacity of their internal edges to , so the algorithm will not delete them (a minimum-cut algorithm computes the minimum cost that makes the graph disconnected, and setting a capacity to makes deleting that node very uneconomical).
Also note that, in addition to the key points mentioned in the statement, the source and sink cannot be deleted either, so this needs to be handled when building the graph. The problem asks for the minimum number of deleted nodes, so the edges connecting these nodes cannot be deleted either; their capacities must be set to .
After resolving the edge-capacity issue, we consider the source and sink. We can set node 1 as the source and connect every key point to the sink. The resulting answer is the minimum number of nodes to delete so that none of the key points can reach node 1 (if any key point can reach node 1, then the sink can also reach node 1).
Summary of the Graph-Building Steps
- Split every node into an incoming node and an outgoing node, with an internal edge between them.
- For a deletable node, set the capacity of its internal edge to .
- For an undeletable node, set the capacity of its internal edge to .
- The undeletable edges include:
- The source’s internal edge.
- The sink’s internal edge.
- The edges connecting every node.
- The internal edges of key points.
- Set the source to node 1 and connect the sink to every key point.
3: Algorithm
I use the Dinic algorithm, because each augmentation can find multiple augmenting paths, making it faster than the EK algorithm. If you are unfamiliar with it, see the solution for the maximum-flow template problem mentioned above.
4: Implementation Details
When implementing node splitting, we can set the number of a node’s incoming point to the node’s own number, and set the number of its outgoing point to its own number plus (the total number of nodes). This ensures that there are no duplicate numbers.
When implementing the Dinic algorithm, we need to operate on reverse edges. I use an STL vector to store edges, so I add a rev (reverse) variable to the node structure to record the index of the current edge’s reverse edge.
1 |
|
This is my first time writing a solution, so there may be many problems. If you find anything incorrect, feel free to point it out in the comments or contact me privately. Questions about anything unclear are also welcome. Finally, if this solution helped you, please give it a like or share your thoughts in the comments.







![[Stanford CS144] Lab 4 Record](/img/CS144/tcp%E7%8A%B6%E6%80%81%E6%B5%81%E8%BD%AC%E5%9B%BE.jpg)