| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 1 초 | 1024 MB | 1 | 1 | 1 | 100.000% |
In compute science, a stack $s$ is a data structure maintaining a list of elements with two operations:
For convenience, Bobo denotes the number of elements in the stack $s$ by $\mathtt{size}(s),ドル and the rightmost element by $\mathtt{right}(s)$.
Bobo has $m$ stacks $s_1, \dots, s_m$. Initially, the stack $s_i$ contains $k_i$ elements $a_{i, 1}, \dots, a_{i, k_i}$ where $a_{i, j} \in \{1, \dots, n\}$. Furthermore, for each $e \in \{1, \dots, n\},ドル the element $e$ occurs in the $m$ stacks exactly twice. Thus, $k_1 + \dots + k_m = 2 n$.
A sorting plan of length $l$ consists of $l$ pairs $(f_1, t_1), \dots, (f_l, t_l)$. To execute a sorting plan, for each $i \in \{1, \dots ,l\}$ in the increasing order, Bobo performs $s_{t_i}.\mathtt{push}(s_{f_i}.\mathtt{pop}())$.
A sorting plan is valid if the length does not exceed $\lfloor \frac{3n}{2} \rfloor,ドル and for each $i \in \{1, \dots, l\},ドル 1ドル \leq f_i, t_i \leq m,ドル $f_i \neq t_i$. Before the $i$-th operation,
Also, after the execution of a valid sorting plan, each of the $m$ stacks either is empty or contains the two copies of the same element.
Find a valid sorting plan, given the initial configuration of the $m$ stacks.
The input consists of several test cases terminated by end-of-file. For each test case,
The first line contains two integers $n$ and $m$.
For the next $m$ lines, the $i$-th line contains an integer $k_i,ドル and $k_i$ integers $a_{i, 1}, \dots, a_{i, k_i}$.
For each test case, if there exists a valid sorting plan, output an integer $l,ドル which denotes the length of the sorting plan. Followed by $l$ lines, the $i$-th line contains two integers $f_i$ and $t_i$. Otherwise, output '-1'.
If there are multiple valid sorting plans, any of them is considered correct.
2 3 2 1 2 2 1 2 0 1 1 2 1 1 3 4 2 1 3 2 2 3 1 1 1 2
3 1 3 2 3 2 1 0 -1
For the first test cases,
For the second test case, the initial configuration is already sorted.