Under construction

Still being built. Things may change

Cactus Construction

Time limit2sMemory limit512 MB

Summary
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 c^\hat{c}. Every vertex carries one color, written as an integer from 11 to c^\hat{c}. The workspace starts with nn separate graphs, one vertex in each. The ii-th graph holds only vertex ii, colored 11, with no edges.

Three operations are permitted.

  • join aa bb: merge the graph containing vertex aa and the graph containing vertex bb into one graph. No edge is added. Vertices aa and bb must lie in different graphs.
  • recolor aa c1c_1 c2c_2: inside the graph containing vertex aa, change every vertex of color c1c_1 to color c2c_2.
  • connect aa c1c_1 c2c_2: inside the graph containing vertex aa, add an edge between every vertex of color c1c_1 and every vertex of color c2c_2. If c1=c2c_1 = c_2, 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 c^\hat{c} 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 44.

You are given a cactus. Assemble it with c^=4\hat{c} = 4 colors, following exactly the construction fixed in the output section.

Input

The first line contains two integers nn and mm (1≤n≤500001 \le n \le 50000; 0≤m≤500000 \le m \le 50000), the number of vertices and the number of paths. Vertices are numbered from 11 to nn. The edges of the graph are given as mm edge-distinct paths.

Each of the next mm lines describes one path. The line starts with an integer kik_i (2≤ki≤10002 \le k_i \le 1000), followed by kik_i vertex numbers between 11 and nn. 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 11. 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 BB, let top(B)\mathrm{top}(B) be the vertex of BB closest to vertex 11; that vertex is unique. Block BB hangs from vertex vv when top(B)=v\mathrm{top}(B) = v.

Order the blocks hanging from vv by the smallest number among the vertices of the block other than vv. Blocks hanging from the same vertex share nothing but vv, so these minima are all different and the order is determined.

Let BB be a cycle block hanging from vv with kk vertices, so k≥3k \ge 3. Vertex vv has exactly two neighbors inside BB. Let w1w_1 be the one with the smaller number, and walk around the cycle from w1w_1 in the direction away from vv. That gives w1,w2,…,wk−1w_1, w_2, \ldots, w_{k-1}, where wk−1w_{k-1} is the other neighbor of vv.

Define the recursive procedure below. In the pseudocode, <x> is replaced by the number of vertex xx, 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 qq on the first line, then the qq lines in that order. For n=1n = 1 nothing is emitted, so print a single line holding 00.

This sequence always assembles the input cactus correctly, it uses only the colors 11 to 44, and qq never exceeds 10610^6.

Note

When build(v) returns, vertex vv has color 11 and every other vertex of the graph containing it has color 22. Colors 33 and 44 are therefore free at every point where a block is wired up, which is why four colors are enough.

Examples3

  1. Example 1

    Input
    8 2
    5 1 2 3 4 7
    5 4 5 6 1 8
    
    Expected output
    29
    r 2 1 3
    r 3 1 4
    j 2 3
    c 2 3 4
    r 7 1 3
    j 4 7
    c 4 1 3
    r 4 3 2
    j 2 4
    c 2 4 1
    r 2 4 2
    r 2 1 4
    j 2 5
    c 2 4 1
    r 2 4 2
    r 2 1 4
    j 2 6
    c 2 4 1
    r 2 4 2
    r 2 1 4
    j 1 2
    c 1 1 3
    c 1 1 4
    r 1 3 2
    r 1 4 2
    r 8 1 3
    j 1 8
    c 1 1 3
    r 1 3 2
    
  2. Example 2

    Input
    15 3
    9 1 2 3 4 5 6 7 8 3
    7 2 9 10 11 12 13 10
    5 2 14 9 15 10
    
    Expected output
    60
    r 4 1 3
    r 5 1 4
    j 4 5
    c 4 3 4
    j 4 6
    c 4 4 1
    r 4 4 2
    r 4 1 4
    j 4 7
    c 4 4 1
    r 4 4 2
    r 4 1 4
    j 4 8
    c 4 4 1
    r 4 4 2
    r 4 1 4
    j 3 4
    c 3 1 3
    c 3 1 4
    r 3 3 2
    r 3 4 2
    r 3 1 3
    j 2 3
    c 2 1 3
    r 2 3 2
    r 11 1 3
    r 12 1 4
    j 11 12
    c 11 3 4
    j 11 13
    c 11 4 1
    r 11 4 2
    r 11 1 4
    j 10 11
    c 10 1 3
    c 10 1 4
    r 10 3 2
    r 10 4 2
    r 10 1 3
    r 15 1 4
    j 10 15
    c 10 3 4
    j 9 10
    c 9 1 3
    c 9 1 4
    r 9 3 2
    r 9 4 2
    r 9 1 3
    r 14 1 4
    j 9 14
    c 9 3 4
    j 2 9
    c 2 1 3
    c 2 1 4
    r 2 3 2
    r 2 4 2
    r 2 1 3
    j 1 2
    c 1 1 3
    r 1 3 2
    
  3. Example 3

    Input
    1 0
    
    Expected output
    0