#abc251f. F - Two Spanning Trees
F - Two Spanning Trees
Score : points
问题描述
给定一个包含 个顶点和 条边的无向图 。 是简单的(不含自环和多条边)且是连通的。
对于 ,第 条边是一条无向边 连接顶点 和 。
构建两个 的生成树 和 ,满足以下两个条件。( 和 不必是不同的生成树。)
-
满足以下条件:
若将 视为以顶点 为根的有根树,则对于 中不属于 的任何边 ,在 中, 和 中必有一个是另一个的祖先。
-
满足以下条件:
若将 视为以顶点 为根的有根树,则不存在不属于 的 中的边 ,使得在 中, 和 中的一个是另一个的祖先。
我们可以证明总存在满足上述条件的 和 。
以上为通义千问 qwen-max 翻译,仅供参考。
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 .
-
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 .
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.
Sample Input 1
6 8
5 1
4 3
1 4
3 5
1 2
2 6
1 6
4 2
Sample Output 1
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.
Sample Input 2
4 3
3 1
1 2
1 4
Sample Output 2
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 .
update @ 2024/3/10 10:43:48