atcoder#ABC251F. [ABC251F] Two Spanning Trees
[ABC251F] Two Spanning Trees
Score : points
Problem Statement
You are given an undirected graph with vertices and edges. is simple (it has no self-loops and multiple edges) and connected.
For , the -th edge is an undirected edge connecting Vertices and .
Construct two spanning trees and of that satisfy both of the two conditions below. ( and do not necessarily have to be different spanning trees.)
-
satisfies the following.
If we regard as a rooted tree rooted at Vertex , for any edge of not contained in , one of and is an ancestor of the other in .
If we regard as a rooted tree rooted at Vertex , for any edge of not contained in , one of and is an ancestor of the other in .
-
satisfies the following.
If we regard as a rooted tree rooted at Vertex , there is no edge of not contained in such that one of and is an ancestor of the other in .
If we regard as a rooted tree rooted at Vertex , there is no edge of not contained in such that one of and is an ancestor of the other in .
We can show that there always exists and that satisfy the conditions above.
Constraints
- $N-1 \leq M \leq \min\lbrace 2 \times 10^5, N(N-1)/2 \rbrace$
- All values in input are integers.
- The given graph is simple and connected.
Input
Input is given from Standard Input in the following format:
Output
Print lines to output and in the following format. Specifically,
- The -st through -th lines should contain the 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 , one edge in each line.
- The -th through -th lines should contain the 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 , 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.
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, is a spanning tree of containing edges $\lbrace 1, 4 \rbrace, \lbrace 4, 3 \rbrace, \lbrace 5, 3 \rbrace, \lbrace 4, 2 \rbrace, \lbrace 6, 2 \rbrace$. This satisfies the condition in the Problem Statement. Indeed, for each edge of not contained in :
- for edge , Vertex is an ancestor of ;
- for edge , Vertex is an ancestor of ;
- for edge , Vertex is an ancestor of .
is another spanning tree of containing edges $\lbrace 1, 5 \rbrace, \lbrace 5, 3 \rbrace, \lbrace 1, 4 \rbrace, \lbrace 2, 1 \rbrace, \lbrace 1, 6 \rbrace$. This satisfies the condition in the Problem Statement. Indeed, for each edge of not contained in :
- for edge , Vertex is not an ancestor of Vertex , and vice versa;
- for edge , Vertex is not an ancestor of Vertex , and vice versa;
- for edge , Vertex is not an ancestor of Vertex , and vice versa.
4 3
3 1
1 2
1 4
1 2
1 3
1 4
1 4
1 3
1 2
Tree , containing edges $\lbrace 1, 2\rbrace, \lbrace 1, 3 \rbrace, \lbrace 1, 4 \rbrace$, is the only spanning tree of . Since there are no edges of that are not contained in , obviously this satisfies the conditions for both and .