Hello 2026
D2
Tree Coloring (Hard Version)
Codeforces
2100
原题
← D1E →
翻译中…

This is the Hard version of the problem. The difference between the versions is that in this version, you must find the minimum number of operations and find a way to color the tree using that many operations. You can hack only if you solved all versions of this problem.

翻译中…

You are given a rooted tree$^{\text{∗}}$ consisting of $n$ vertices numbered from $1$ to $n$, where the root has index $1$, and each vertex is initially white. Define $d_i$ as the distance from the root to the $i$-th vertex. You can perform the following operations any number of times:

翻译中…
  1. Select a subset $S$ of white vertices such that no two nodes in $S$ are connected by an edge, or have the same distance to node $1$. Formally, $S$ should satisfy for all $x,y\in S$ and $x\neq y$, $d_x\neq d_y$, and there are no edges between $x$ and $y$.
  2. Color the vertices in $S$ black.
翻译中…

Your job is to find the minimum number of operations required to color the full tree black and find a way to perform the operations.

翻译中…

$^{\text{∗}}$A tree is a connected graph without cycles. A rooted tree is a tree where one vertex is special and called the root.

翻译中…

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 a single integer $n$ ($2\le n\le 2\cdot 10^5$) – the number of vertices in the tree.

翻译中…

The $i$-th following $n-1$ lines contain two integers $u_i$ and $v_i$ ($1\le u_i,v_i\le n$, $u_i\neq v_i$) — the ends of the $i$-th edge.

翻译中…

It is guaranteed that the given edges form a tree.

翻译中…

It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2\cdot10^5$.

翻译中…

Output

翻译中…

For each test case:

翻译中…
  • Output your number of operations $k$ on the first line ($1 \leq k \leq n$).
  • Then, output $k$ lines. Each line should begin with an integer $m$ representing the size of the operation's set ($0 \leq m \leq n$). After that, there should be $m$ numbers $u_1,u_2,\ldots,u_m$ ($1 \leq u_i \leq n$) representing the $m$ nodes you color black in this operation.
翻译中…

You should guarantee that:

翻译中…
  • Each operation you make is valid.
  • You don't color the same vertex twice (in the same operation or in different operations).
  • At the end of all operations, all vertices are colored black.
  • The number of operations you performed is $x$, where $x$ is the minimum possible number of operations over all possible solutions.
翻译中…

If there are multiple possible solutions, output any.

翻译中…

Note

翻译中…

In the first test case, $d_1=1$ and $d_2=d_3=d_4=d_5=2$. We can show that we must perform at least $5$ operations because there are no two nodes that can be operated on simultaneously.

翻译中…

In the second test case, we can show that the least number of operations required to color the full tree is $4$. The example output shows one way to color it in $4$ operations.

翻译中…

样例

Input #1
10
5
3 1
1 2
5 1
4 1
5
3 2
2 4
2 5
1 2
5
3 4
4 1
5 1
1 2
5
2 5
3 1
2 1
3 4
5
1 3
1 5
4 3
2 4
13
2 1
3 2
4 2
5 4
6 3
7 1
8 5
9 6
10 4
11 7
12 8
13 10
10
5 7
8 1
1 10
2 8
8 4
9 4
6 1
5 3
7 8
10
7 6
3 7
6 9
7 1
9 8
5 1
3 10
9 2
1 4
10
10 6
2 8
4 10
7 5
1 2
7 10
10 9
9 1
7 3
10
6 8
9 7
4 10
5 9
4 2
3 8
6 5
1 5
1 10
Output #1
5
1 3
1 2
1 5
1 4
1 1
4
2 3 1
1 4
1 5
1 2
4
1 4
2 5 3
1 2
1 1
3
2 4 2
2 5 3
1 1
3
2 3 2
2 5 4
1 1
3
5 9 12 10 11 2
4 8 6 4 1
4 13 5 3 7
4
4 2 9 3 10
3 4 5 6
2 7 1
1 8
4
2 7 9
3 5 3 2
4 4 6 10 8
1 1
4
4 6 3 8 9
3 4 5 1
1 7
2 10 2
3
4 7 3 4 5
3 8 9 10
3 2 6 1
InputCopy OutputCopy
题目信息
题号
2183D2
所属比赛
时间限制
2 s
内存限制
512 MB
标签
组合数学
构造
数据结构
DFS 及类似
贪心
模拟
树

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