Data Structure

시간 제한1초메모리 제한1024 MB

요약
1부터 n까지 각 값의 사본 두 개를 담은 m개의 스택이 주어질 때, 용량 규칙을 지키며 같은 값끼리 한 스택에 모으는 이동 순서를 찾는다.
난이도

어려움10점 중 8점

유형
스택, 그래프, 구현, 그리디
정답자
아직 제출이 없습니다

문제

In compute science, a stack ss is a data structure maintaining a list of elements with two operations:

  1. s.push(e)s.\mathtt{push}(e) appends an element ee to the right end of the list,
  2. s.pop()s.\mathtt{pop}() removes the rightmost element in the list and returns the removed element.

For convenience, Bobo denotes the number of elements in the stack ss by size(s)\mathtt{size}(s), and the rightmost element by right(s)\mathtt{right}(s).

Bobo has mm stacks s_1,…,s_ms\_1, \dots, s\_m. Initially, the stack s_is\_i contains k_ik\_i elements a_i,1,…,a_i,k_ia\_{i, 1}, \dots, a\_{i, k\_i} where a_i,j∈1,…,na\_{i, j} \in \\{1, \dots, n\\}. Furthermore, for each e∈1,…,ne \in \\{1, \dots, n\\}, the element ee occurs in the mm stacks exactly twice. Thus, k_1+⋯+k_m=2nk\_1 + \dots + k\_m = 2 n.

A sorting plan of length ll consists of ll pairs (f_1,t_1),…,(f_l,t_l)(f\_1, t\_1), \dots, (f\_l, t\_l). To execute a sorting plan, for each i∈1,…,li \in \\{1, \dots ,l\\} in the increasing order, Bobo performs s_t_i.push(s_f_i.pop())s\_{t\_i}.\mathtt{push}(s\_{f\_i}.\mathtt{pop}()).

A sorting plan is valid if the length does not exceed ⌊3n2⌋\lfloor \frac{3n}{2} \rfloor, and for each i∈1,…,li \in \\{1, \dots, l\\}, 1≤f_i,t_i≤m1 \leq f\_i, t\_i \leq m, f_i≠t_if\_i \neq t\_i. Before the ii-th operation,

  • size(s_f_i)>0\mathtt{size}(s\_{f\_i}) > 0,
  • size(s_t_i)<2\mathtt{size}(s\_{t\_i}) < 2,
  • either size(s_t_i)=0\mathtt{size}(s\_{t\_i}) = 0 or right(s_f_i)=right(s_t_i)\mathtt{right}(s\_{f\_i}) = \mathtt{right}(s\_{t\_i}).

Also, after the execution of a valid sorting plan, each of the mm stacks either is empty or contains the two copies of the same element.

Find a valid sorting plan, given the initial configuration of the mm stacks.

입력

The input consists of several test cases terminated by end-of-file. For each test case,

The first line contains two integers nn and mm.

For the next mm lines, the ii-th line contains an integer k_ik\_i, and k_ik\_i integers a_i,1,…,a_i,k_ia\_{i, 1}, \dots, a\_{i, k\_i}.

출력

For each test case, if there exists a valid sorting plan, output an integer ll, which denotes the length of the sorting plan. Followed by ll lines, the ii-th line contains two integers f_if\_i and t_it\_i. Otherwise, output '-1'.

If there are multiple valid sorting plans, any of them is considered correct.

제한

  • 1≤n≤m≤2×1051 \le n \leq m \le 2 \times 10^5
  • 0≤k_i≤20 \leq k\_i \leq 2 for each 1≤i≤m1 \leq i \leq m
  • 1≤a_i,j≤n1 \leq a\_{i, j} \leq n for each 1≤i≤m1 \leq i \leq m, 1≤j≤k_i1 \leq j \leq k\_i
  • For each 1≤e≤n1 \leq e \leq n, there exists exactly two (i,j)(i, j) where 1≤j≤k_i1 \leq j \leq k\_i and a_i,j=ea\_{i, j} = e.
  • In each input, the sum of mm does not exceed 2×1052 \times 10^5.

힌트

For the first test cases,

  • Initially, s_1=\[1,2]s\_1 = \[1, 2], s_2=\[1,2]s\_2 = \[1, 2], s_3=\[ ]s\_3 = \[\ ].
  • After s_3.push(s_1.pop())s\_3.\mathtt{push}(s\_1.\mathtt{pop}()). s_1=\[1]s\_1 = \[1], s_2=\[1,2]s\_2 = \[1, 2], s_3=\[2]s\_3 = \[2].
  • After s_3.push(s_2.pop())s\_3.\mathtt{push}(s\_2.\mathtt{pop}()), s_1=\[1]s\_1 = \[1], s_2=\[1]s\_2 = \[1], s_3=\[2,2]s\_3 = \[2, 2].
  • After s_1.push(s_2.pop())s\_1.\mathtt{push}(s\_2.\mathtt{pop}()), s_1=\[1,1]s\_1 = \[1, 1], s_2=\[ ]s\_2 = \[\ ], s_3=\[2,2]s\_3 = \[2, 2].

For the second test case, the initial configuration is already sorted.

예제1

  1. 예제 1

    입력
    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