This blog post is a submission for the Codeforces Month of Blog Posts Pt. III challenge. Thank you cadmiumky for the initiative!
Introduction Problem
Before tackling the problem, let's first try some simple examples.
The notation we use will also be introduced in the examples, so please try to understand them carefully.
Enough examples, let's try to solve the problem now.
Implementation

Code: https://qoj.ac/submission/2028368
Depends on problem requirement, the Info struct and the three operations Prune, Twist and Compress may be defined differently. Basically, the Info struct should store the information of a cluster, and the three operations should be defined according to the meaning of the information stored in the Info struct.
For simplicity, we will use vector<map<int, Info>> to store the adjacency lists and the info on the edge to represent the graph, and use two queues to maintain the nodes with degree $$$1$$$ or $$$2$$$.
Here is the pseudo code of the reduction process:
Generalization
General Series-Parallel Graphs (GSPG)
If a graph can be constructed by repeatedly applying the following operations starting from a single node, then it is called a General Series-Parallel Graph.
- Add a new isolated node.
- Add a new node with an edge connecting it to any existing node.
- It is the inverse operation of prune.
- Duplicate any existing edge by adding a new edge between the same pair of nodes.
- It is the inverse operation of twist.
- Subdivide any existing edge by adding a new node in the middle of the edge.
- It is the inverse operation of compress.
Trees are GSPG, cactuses are GSPG, outer planar graphs are GSPG!
If a graph is a GSPG, then we can apply the reduction process to it until there is only one node left.
Connected Graphs with Small $$$m - n$$$
Observe the operations:
- in Prune operation, $$$n \gets n - 1$$$ and $$$m \gets m - 1$$$;
- in Twist operation, $$$n \gets n$$$ and $$$m \gets m - 1$$$;
- in Compress operation, $$$n \gets n - 1$$$ and $$$m \gets m - 1$$$.
The value of $$$m - n$$$ will never increase and will decrease by $$$1$$$ per Twist operations.
When the reduction process stops, we have the degree of each node at least $$$3$$$, so we can yield $$$2m' \ge 3n'$$$.
Suppose $$$m - n \le k$$$ in the original graph, then we have $$$m' - n' \le k$$$ and $$$2m' \ge 3n'$$$, which yields $$$n' \le 2k$$$ and $$$m' \le 3k$$$.
Thus, if $$$m - n$$$ is small, we can apply the reduction process until there are at most $$$2k$$$ nodes and $$$3k$$$ edges left, and then solve the problem by brute force.
Values on Boundary Nodes
Sometimes, we may want to maintain some values on the boundary nodes of the cluster, and the operations should also update these values together with the information on the edge.
Practice
If you know some problem that can be solved by the method, please share it in the comments.
(I will try to put some problems on polygon when I have time...)
Notes: There are subtasks to test the solution without modifications.
References
- https://github.com/enkerewpo/OI-Public-Library/blob/master/IOI%E4%B8%AD%E5%9B%BD%E5%9B%BD%E5%AE%B6%E5%80%99%E9%80%89%E9%98%9F%E8%AE%BA%E6%96%87/%E5%9B%BD%E5%AE%B6%E9%9B%86%E8%AE%AD%E9%98%9F2019%E8%AE%BA%E6%96%87%E9%9B%86.pdf, page 165-191 (Chinese)
- https://zhuanlan.zhihu.com/p/32413108 (Chinese)
- https://oi-wiki.org/ds/top-tree/ (Chinese)
- https://en.wikipedia.org/wiki/Series%E2%80%93parallel_graph

















