atcoder#ABC251F. [ABC251F] Two Spanning Trees

[ABC251F] Two Spanning Trees

Score : 500500 points

Problem Statement

You are given an undirected graph GG with NN vertices and MM edges. GG is simple (it has no self-loops and multiple edges) and connected.

For i=1,2,,Mi = 1, 2, \ldots, M, the ii-th edge is an undirected edge {ui,vi}\lbrace u_i, v_i \rbrace connecting Vertices uiu_i and viv_i.

Construct two spanning trees T1T_1 and T2T_2 of GG that satisfy both of the two conditions below. (T1T_1 and T2T_2 do not necessarily have to be different spanning trees.)

  • T1T_1 satisfies the following.

    If we regard T1T_1 as a rooted tree rooted at Vertex 11, for any edge {u,v}\lbrace u, v \rbrace of GG not contained in T1T_1, one of uu and vv is an ancestor of the other in T1T_1.

    If we regard T1T_1 as a rooted tree rooted at Vertex 11, for any edge {u,v}\lbrace u, v \rbrace of GG not contained in T1T_1, one of uu and vv is an ancestor of the other in T1T_1.

  • T2T_2 satisfies the following.

    If we regard T2T_2 as a rooted tree rooted at Vertex 11, there is no edge {u,v}\lbrace u, v \rbrace of GG not contained in T2T_2 such that one of uu and vv is an ancestor of the other in T2T_2.

    If we regard T2T_2 as a rooted tree rooted at Vertex 11, there is no edge {u,v}\lbrace u, v \rbrace of GG not contained in T2T_2 such that one of uu and vv is an ancestor of the other in T2T_2.

We can show that there always exists T1T_1 and T2T_2 that satisfy the conditions above.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • $N-1 \leq M \leq \min\lbrace 2 \times 10^5, N(N-1)/2 \rbrace$
  • 1ui,viN1 \leq u_i, v_i \leq N
  • All values in input are integers.
  • The given graph is simple and connected.

Input

Input is given from Standard Input in the following format:

NN MM

u1u_1 v1v_1

u2u_2 v2v_2

\vdots

uMu_M vMv_M

Output

Print (2N2)(2N-2) lines to output T1T_1 and T2T_2 in the following format. Specifically,

  • The 11-st through (N1)(N-1)-th lines should contain the (N1)(N-1) undirected edges $\lbrace x_1, y_1\rbrace, \lbrace x_2, y_2\rbrace, \ldots, \lbrace x_{N-1}, y_{N-1}\rbrace$ contained in T1T_1, one edge in each line.
  • The NN-th through (2N2)(2N-2)-th lines should contain the (N1)(N-1) undirected edges $\lbrace z_1, w_1\rbrace, \lbrace z_2, w_2\rbrace, \ldots, \lbrace z_{N-1}, w_{N-1}\rbrace$ contained in T2T_2, one edge in each line.

You may print edges in each spanning tree in any order. Also, you may print the endpoints of each edge in any order.

x1x_1 y1y_1

x2x_2 y2y_2

\vdots

xN1x_{N-1} yN1y_{N-1}

z1z_1 w1w_1

z2z_2 w2w_2

\vdots

zN1z_{N-1} wN1w_{N-1}

6 8
5 1
4 3
1 4
3 5
1 2
2 6
1 6
4 2
1 4
4 3
5 3
4 2
6 2
1 5
5 3
1 4
2 1
1 6

In the Sample Output above, T1T_1 is a spanning tree of GG containing 55 edges $\lbrace 1, 4 \rbrace, \lbrace 4, 3 \rbrace, \lbrace 5, 3 \rbrace, \lbrace 4, 2 \rbrace, \lbrace 6, 2 \rbrace$. This T1T_1 satisfies the condition in the Problem Statement. Indeed, for each edge of GG not contained in T1T_1:

  • for edge {5,1}\lbrace 5, 1 \rbrace, Vertex 11 is an ancestor of 55;
  • for edge {1,2}\lbrace 1, 2 \rbrace, Vertex 11 is an ancestor of 22;
  • for edge {1,6}\lbrace 1, 6 \rbrace, Vertex 11 is an ancestor of 66.

T2T_2 is another spanning tree of GG containing 55 edges $\lbrace 1, 5 \rbrace, \lbrace 5, 3 \rbrace, \lbrace 1, 4 \rbrace, \lbrace 2, 1 \rbrace, \lbrace 1, 6 \rbrace$. This T2T_2 satisfies the condition in the Problem Statement. Indeed, for each edge of GG not contained in T2T_2:

  • for edge {4,3}\lbrace 4, 3 \rbrace, Vertex 44 is not an ancestor of Vertex 33, and vice versa;
  • for edge {2,6}\lbrace 2, 6 \rbrace, Vertex 22 is not an ancestor of Vertex 66, and vice versa;
  • for edge {4,2}\lbrace 4, 2 \rbrace, Vertex 44 is not an ancestor of Vertex 22, and vice versa.
4 3
3 1
1 2
1 4
1 2
1 3
1 4
1 4
1 3
1 2

Tree TT, containing 33 edges $\lbrace 1, 2\rbrace, \lbrace 1, 3 \rbrace, \lbrace 1, 4 \rbrace$, is the only spanning tree of GG. Since there are no edges of GG that are not contained in TT, obviously this TT satisfies the conditions for both T1T_1 and T2T_2.