Cactus Construction
Time limit2sMemory limit512 MB
Given a cactus as edge-disjoint paths, simulate the fixed recursive procedure that emits join, recolor, and connect operations assembling it with four colors.
- Level
Medium6 of 10
- Topics
- Graph, DFS, Recursion, Implementation
- Solved
- No attempts yet
Problem
A cactus is a connected undirected graph in which every edge lies on at most one simple cycle. A cactus has no loops and no multiple edges. It is a tree that is allowed to contain some cycles.
Graphs can be assembled by the following process. Fix a number of colors . Every vertex carries one color, written as an integer from to . The workspace starts with separate graphs, one vertex in each. The -th graph holds only vertex , colored , with no edges.
Three operations are permitted.
- join : merge the graph containing vertex and the graph containing vertex into one graph. No edge is added. Vertices and must lie in different graphs.
- recolor : inside the graph containing vertex , change every vertex of color to color .
- connect : inside the graph containing vertex , add an edge between every vertex of color and every vertex of color . If , no loop is created. If such an edge already exists, a second parallel edge would appear. Multiple edges are not allowed in this problem, so that case must never occur.
Once the last operation is applied, the workspace holds a single graph, and the colors of its vertices no longer matter.
The smallest that suffices to assemble a graph is called the clique width of that graph. On graphs of bounded clique width, many NP-hard problems can be solved in polynomial time by dynamic programming over this assembly process. Computing the exact clique width of a general graph is NP-hard, but bounds are known for some graph classes. The clique width of a cactus is at most .
You are given a cactus. Assemble it with colors, following exactly the construction fixed in the output section.
Input
The first line contains two integers and (; ), the number of vertices and the number of paths. Vertices are numbered from to . The edges of the graph are given as edge-distinct paths.
Each of the next lines describes one path. The line starts with an integer (), followed by vertex numbers between and . Adjacent vertices on a line are different. A path may visit the same vertex several times, and every edge of the graph appears exactly once across the whole input.
The graph given this way is a cactus.
Output
Many different operation sequences assemble the same cactus, so this problem fixes one of them.
Root the cactus at vertex . Every edge of a cactus belongs to exactly one block, where a block is either a simple cycle or a bridge, that is, an edge that lies on no cycle. Two different blocks share at most one vertex. For a block , let be the vertex of closest to vertex ; that vertex is unique. Block hangs from vertex when .
Order the blocks hanging from by the smallest number among the vertices of the block other than . Blocks hanging from the same vertex share nothing but , so these minima are all different and the order is determined.
Let be a cycle block hanging from with vertices, so . Vertex has exactly two neighbors inside . Let be the one with the smaller number, and walk around the cycle from in the direction away from . That gives , where is the other neighbor of .
Define the recursive procedure below. In the pseudocode, <x> is replaced by the number of vertex , and every emitted string is one output line.
build(v):
for each block B hanging from v, in the order defined above:
if B is the bridge {v, u}:
build(u)
emit "r <u> 1 3"
emit "j <v> <u>"
emit "c <v> 1 3"
emit "r <v> 3 2"
else B is the cycle v, w[1], w[2], ..., w[k-1]:
build(w[1])
emit "r <w[1]> 1 3"
build(w[2])
emit "r <w[2]> 1 4"
emit "j <w[1]> <w[2]>"
emit "c <w[1]> 3 4"
for i = 3, 4, ..., k-1:
build(w[i])
emit "j <w[1]> <w[i]>"
emit "c <w[1]> 4 1"
emit "r <w[1]> 4 2"
emit "r <w[1]> 1 4"
emit "j <v> <w[1]>"
emit "c <v> 1 3"
emit "c <v> 1 4"
emit "r <v> 3 2"
emit "r <v> 4 2"
Run build(1) and collect the emitted lines in order. Print their count on the first line, then the lines in that order. For nothing is emitted, so print a single line holding .
This sequence always assembles the input cactus correctly, it uses only the colors to , and never exceeds .
Note
When build(v) returns, vertex has color and every other vertex of the graph containing it has color . Colors and are therefore free at every point where a block is wired up, which is why four colors are enough.