#P16921. [JLCPC 2026] 拆分树

    ID: 19239 远端评测题 8000ms 1024MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>吉林Special JudgeO2优化2026省赛/邀请赛

[JLCPC 2026] 拆分树

Problem Description

tarjen\mathit{tarjen} is playing a game of merging two trees into one graph. He took two trees with nn vertices each and merged them into an undirected unweighted graph G=(V,E)G = (V, E) with nn vertices and m=2n−2m = 2n - 2 edges. However, he found that he cannot restore this graph back into two trees. Please help him.

Formally, you need to partition all edges into two sets TT and E∖TE \setminus T, such that both TT and E∖TE \setminus T form a spanning tree of GG.

You may output any valid solution. It is guaranteed that a valid solution always exists.

Note: The graph may contain multiple edges, but it is guaranteed to have no self-loops.

Input Format

The first line contains an integer TT (1≤T≤50001 \le T \le 5000), the number of test cases. Then follow TT test cases, each described as follows.

  • The first line contains an integer nn (2≤n≤50002 \le n \le 5000), the number of vertices.
  • The next 2n−22n - 2 lines each contain two integers u,vu, v (1≤u,v≤n1 \le u, v \le n, u≠vu \ne v), describing an edge. Edges are numbered from 11 to 2n−22n - 2 in the input order.

It is guaranteed that ∑n\sum n does not exceed 1000010000.

Output Format

For each test case, output n−1n - 1 integers (in increasing order), indicating the edge indices included in the first spanning tree. The remaining n−1n - 1 edges should also form a spanning tree.

2
3
1 2
2 3
1 3
1 2
4
1 2
2 3
3 4
1 4
1 3
2 4
1 2
1 2 3

Hint

For the first sample, edges {1,2}\{1, 2\} correspond to {(1,2),(2,3)}\{(1,2), (2,3)\} and form a spanning tree. The remaining edges {3,4}\{3, 4\} correspond to {(1,3),(1,2)}\{(1,3), (1,2)\} and also form a spanning tree.

For the second sample, edges {1,2,3}\{1, 2, 3\} correspond to {(1,2),(2,3),(3,4)}\{(1,2), (2,3), (3,4)\} and form a spanning tree. The remaining edges {4,5,6}\{4, 5, 6\} correspond to {(1,4),(1,3),(2,4)}\{(1,4), (1,3), (2,4)\} and also form a spanning tree.

Translated by ChatGPT 5