atcoder#AGC029F. [AGC029F] Construction of a tree

[AGC029F] Construction of a tree

Score : 22002200 points

Problem Statement

You are given N1N-1 subsets of {1,2,...,N}\{1,2,...,N\}. Let the ii-th set be EiE_i.

Let us choose two distinct elements uiu_i and viv_i from each set EiE_i, and consider a graph TT with NN vertices and N1N-1 edges, whose vertex set is {1,2,..,N}\{1,2,..,N\} and whose edge set is (u1,v1),(u2,v2),...,(uN1,vN1)(u_1,v_1),(u_2,v_2),...,(u_{N-1},v_{N-1}). Determine if TT can be a tree by properly deciding uiu_i and viv_i. If it can, additionally find one instance of (u1,v1),(u2,v2),...,(uN1,vN1)(u_1,v_1),(u_2,v_2),...,(u_{N-1},v_{N-1}) such that TT is actually a tree.

Constraints

  • 2N1052 \leq N \leq 10^5
  • EiE_i is a subset of {1,2,..,N}\{1,2,..,N\}.
  • Ei2|E_i| \geq 2
  • The sum of Ei|E_i| is at most 2×1052 \times 10^5.

Input

Input is given from Standard Input in the following format:

NN

c1c_1 w1,1w_{1,1} w1,2w_{1,2} ...... w1,c1w_{1,c_1}

::

cN1c_{N-1} wN1,1w_{N-1,1} wN1,2w_{N-1,2} ...... wN1,cN1w_{N-1,c_{N-1}}

Here, cic_i stands for the number of elements in EiE_i, and wi,1,...,wi,ciw_{i,1},...,w_{i,c_i} are the cic_i elements in cic_i. Here, 2ciN2 \leq c_i \leq N, 1wi,jN1 \leq w_{i,j} \leq N, and wi,jwi,kw_{i,j} \neq w_{i,k} (1j<kci1 \leq j < k \leq c_i) hold.

Output

If TT cannot be a tree, print -1; otherwise, print the choices of (ui,vi)(u_i,v_i) that satisfy the condition, in the following format:

u1u_1 v1v_1

::

uN1u_{N-1} vN1v_{N-1}

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