#P6987. [NEERC2014] Cactus Generator(征集SPJ)

[NEERC2014] Cactus Generator(征集SPJ)

题目描述

NEERC featured a number of problems about cactuses -- connected undirected graphs in which every edge belongs to at most one simple cycle. Intuitively, cactus is a generalization of a tree where some cycles are allowed.

In 20052005 , the first year where a problem about cactuses appeared, the problem was called simply Cactus. In 20072007 it was Cactus Reloaded, in 20102010 it was called Cactus Revolution, and in 20132013 it was called Cactus Automorphisms. Here is an example of cactus that was used in those problems:

For four years judges had to generate test files for cactuses with thousands of vertices. Of course, a number of test generators of ever-increasing complexity were built, culminating with a domain-specific language called CGL -- Cactus Generator Language. CGL can compactly define a big cactus for purposes of a test. In this problem you have to parse a simplified version of this language, which we call SCGL -- Simple Cactus Generator Language, and output a resulting cactus.

A cactus has to be output by listing the minimal set of edge-distinct paths that cover the whole graph.

The syntax of SCGL cactus definition is represented by the graph non-terminal in the grammar that is given below using the Extended Backus-Naur Form:

graph == c

| c( list )

| loop( list )

| t( list )

list == graph { , graph }

| (number | range | variable ) [[ , graph ]]

number == nzdig { 0 | nzdig }

nzdig == 1 | 2 | \cdots | 8 | 9

range == range( variable , numvar , numvar )

variable == A | B | \cdots | Y | Z

numvar == number | variable

A graph production rule denotes a graph with two labeled vertices -- the first and the last. Graphs definition rules have the following semantics:

The basic building block cc denotes a graph with just two vertices (one is the first and the other one is the last) and one edge.

The c(σ)c(σ) rule connects a specified list of graphs σσ from left to right into a chain, merging the last vertex of the first graph in the list with the first vertex of the second graph in the list, the last vertex of the second graph with the first of the third one, and so on. The resulting graph's first vertex is the first vertex of the first graph in the list, and the resulting graph's last vertex is the last vertex of the last graph in the list.

The loop(σ)loo_p(σ) rule connects a specified list of graphs σσ from left to right, merging the last vertex of the first graph in the list with the first vertex of the second graph in the list, and so on like in c(σ),c(σ), while the last vertex of the last graph in the list is merged with the first vertex of the first graph in the list to form a loop. The resulting graph's first and last vertices are the first and the last vertices of the first graph in the list. Loop can be applied only to lists with more than one graph.

The t(σ)t(σ) rule connects a specified list of graphs σ,σ, merging their first vertices. The resulting graph's first and last vertices are the first and the last vertices of the first graph in the list.

The list of graphs is either specified explicitly, by a comma-separated list, or using a list repetition with a number, a range, or a variable, optionally followed by a comma and a graph. When a graph is not explicitly specified in a list repetition, then the given graph is assumed to be cc .

The simplest list repetition is defined using a number non-terminal. It denotes a list of graphs with the specified integer number of copies of the given graph.

A range list repetition is defined by range(ν,α,β)range(ν, α, β) rule which has three components -- a variable ν,ν, and numbers αα and β.β. If ξξ character sequence is a graph, then cloopt(range(ν,α,β),ξ)c|loo_p|t(range(ν, α, β), ξ) are called rangeenabled rules and the variable νν is called a bound variable in ξ.ξ. In the context of a range-enabled rule, ξξ is repeated βα+1|β − α| + 1 times to form a list. Every occurrence of variable νν in ξξ is replaced by consecutive integer numbers between αα and ββ inclusive in ascending order. That produces a list of βα+1|β −α|+ 1 graphs, which are then connected according the specification of the corresponding range-enabled rule. The αα and ββ themselves might refer to variables that are bound in the outer range-enabled rule.

In a well-formed graph:

each variable non-terminal (a letter from A to Z)Z) occurs at most once as νν in range(ν,α,β)range(ν, α, β) rules;

all other occurrences of variable non-terminal that are allowed by the grammar are bound.

Note, that if a character sequence ξξ is a graph, then ξ,c(ξ),ξ, c(ξ), c(1 , ξ),t(ξ),ξ), t(ξ), and t(1 , ξ)ξ) all denote the same graph. On the other hand, neither loop(ξ)loo_p(ξ) nor loop(1 , ξ)ξ) are allowed.

The following examples illustrate these basic rules. The graphs have their first and last vertices marked with letters FF and LL correspondingly.

输入格式

The input file contains a single line with a well-formed cactus definition in SCGL. While the syntax and semantics of SCGL themselves do not guarantee that the resulting graph is a cactus, the input file for this problem always defines a cactus -- every edge belongs to at most one simple cycle and there are no multiple edges between vertices. For example, neither loop(3 , loop(3))loo_p(3)) nor loop(2)loo_p(2) are possible.

The line in the input file is at most 10001000 characters long and defines a cactus with at most 5000050 000 vertices. Integer numbers represented by number non-terminals do not exceed 5000050 000 .

输出格式

The first line of the output file must contain two integer numbers nn and mm . Here nn is the number of vertices in the graph. Vertices are numbered from 11 to nn , where 11 is the number of the first vertex of the graph and nn is the number of the last vertex of the graph. The other vertices can be numbered arbitrarily. Edges of the graph are represented by a set of edge-distinct paths, where mm is the minimal number of such paths.

Each of the following mm lines must contain a path in the graph. A path starts with an integer number ki(ki2)k_{i} (k_{i} \ge 2) followed by kik_{i} integers from 11 to nn . These kik_{i} integers represent vertices of a path. A path can go to the same vertex multiple times, but every edge must be traversed exactly once in the whole output file.

c(c,t(loop(3),c(c,loop(6))),loop(c,c,t(c,loop(4))))

15 1
19 1 2 9 10 11 12 13 10 15 9 14 2 3 4 5 6 7 8 3

c

2 1
2 1 2

c(2)

3 1
3 1 2 3

c(3)

4 1
4 1 2 3 4

t(c(3),c,c)

6 2
2 1 2
5 3 1 4 5 6

c(2,t(c(2),c,c))

9 3
3 2 1 3
3 4 5 6
5 1 7 5 8 9

提示

Time limit: 1 s, Memory limit: 256 MB.