Codeforces Round 1022 (Div. 2)
E
Spruce Dispute
Codeforces
2600
原题
← DF →
翻译中…

It's already a hot April outside, and Polycarp decided that this is the perfect time to finally take down the spruce tree he set up several years ago. As he spent several hours walking around it, gathering his strength, he noticed something curious: the spruce is actually a tree$^{\text{∗}}$ — and not just any tree, but one consisting of an odd number of vertices $n$. Moreover, on $n-1$ of the vertices hang Christmas ornaments, painted in exactly $\frac{n-1}{2}$ distinct colors, with exactly two ornaments painted in each color. The remaining vertex, as tradition dictates, holds the tree's topper.

翻译中…

At last, after several days of mental preparation, Polycarp began dismantling the spruce. First, he removed the topper and had already started taking apart some branches when suddenly a natural question struck him: how can he remove one of the tree's edges and rearrange the ornaments in such a way that the sum of the lengths of the simple paths between ornaments of the same color is as large as possible?

翻译中…

In this problem, removing an edge from the tree is defined as follows: choose a pair of adjacent vertices $a$ and $b$ ($a \lt b$), then remove vertex $b$ from the tree and reattach all of $b$'s adjacent vertices (except for $a$) directly to $a$.

翻译中…

Polycarp cannot continue dismantling his spruce until he gets an answer to this question. However, checking all possible options would take him several more years. Knowing your experience in competitive programming, he turned to you for help. But can you solve this dispute?

翻译中…

$^{\text{∗}}$A tree is a connected graph without cycles.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains an odd number $n$ ($3 \le n \lt 2 \cdot 10^5$) — the number of vertices in the tree.

翻译中…

The following $n-1$ lines describe the tree, given by pairs of adjacent vertices $u$, $v$ ($1 \le u, v \le n, u \neq v$).

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, you need to output two lines.

翻译中…

In the first line, output the pair of vertices $u$, $v$, the edge between which Polycarp is going to remove.

翻译中…

In the next line, output the array $c$ of $n$ numbers from $0$ to $\frac{n-1}{2}$, where $c[i]$ — the positive color number assigned to vertex $i$. Note that $c[\text{max}(u, v)]= 0$, since this vertex has been removed.

翻译中…

Note

翻译中…

Consider the first test case.

翻译中…

Remove the edge connecting vertices $1$ and $2$. After this, vertex $2$ will be removed from the tree, and vertices $3$ and $4$ will be connected to vertex $1$.

翻译中…

Color vertices $3$ and $4$ with the first color, and vertices $1$ and $5$ with the second. The sum of the lengths of simple paths between ornaments of the same color is equal to $2 + 2 = 4$. It can be shown that this value is the largest possible.

翻译中…

image

翻译中…

In the second and third examples, the maximum sum of path lengths will be $3$ and $9$, respectively.

翻译中…

样例

Input #1
3
5
1 2
2 3
2 4
4 5
5
1 2
1 3
1 4
1 5
7
1 5
5 4
4 3
3 2
2 6
6 7
Output #1
1 2
2 0 1 1 2
1 5
1 1 2 2 0
4 3
1 3 3 0 2 2 1
InputCopy OutputCopy
题目信息
题号
2108E
时间限制
2 s
内存限制
256 MB
标签
构造
DFS 及类似
图论
贪心
模拟
最短路
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。