CF2183D2.Tree Coloring (Hard Version)
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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∗ consisting of n vertices numbered from 1 to n, where the root has index 1, and each vertex is initially white. Define di as the distance from the root to the i-th vertex. You can perform the following operations any number of times:
- 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∈S and x=y, dx=dy, and there are no edges between x and y.
- 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.
∗A tree is a connected graph without cycles. A rooted tree is a tree where one vertex is special and called the root.
这是本题的困难版本。两个版本的区别在于:在本版本中,你不仅要找出所需的最少操作次数,还必须给出一种使用该最少次数完成染色的具体方案。只有当你已解决本题的所有版本后,才可进行 Hack。
给定一棵有根树∗,包含 n 个顶点,编号为 1 到 n,其中根节点编号为 1,且每个顶点初始均为白色。定义 di 为第 i 个顶点到根节点的距离。你可以执行以下操作任意多次:
- 选择一个白色顶点子集 S,使得 S 中任意两个顶点之间既不相邻(即不存在边连接),也不具有相同的到节点 1 的距离。形式化地,对所有 x,y∈S 且 x=y,需满足 dx=dy,且 x 与 y 之间无边相连。
- 将子集 S 中的所有顶点染成黑色。
你的任务是:求出将整棵树全部染成黑色所需的最少操作次数,并给出一种实现该最少次数的操作方案。
∗树是一个无环的连通图;有根树是一棵指定某一顶点为根的树。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer n (2≤n≤2⋅105) – the number of vertices in the tree.
The i-th following n−1 lines contain two integers ui and vi (1≤ui,vi≤n, ui=vi) — 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⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)——树中顶点的数量。
接下来的 n−1 行中,第 i 行包含两个整数 ui 和 vi(1≤ui,vi≤n,且 ui=vi)——第 i 条边的两个端点。
保证所给的边构成一棵树。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
For each test case:
- Output your number of operations k on the first line (1≤k≤n).
- Then, output k lines. Each line should begin with an integer m representing the size of the operation's set (0≤m≤n). After that, there should be m numbers u1,u2,…,um (1≤ui≤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.
对于每个测试用例:
- 在第一行输出你的操作次数 k(1≤k≤n);
- 接着输出 k 行,每行首先是一个整数 m,表示该次操作所选顶点集合的大小(0≤m≤n);随后是 m 个整数 u1,u2,…,um(1≤ui≤n),表示本次操作中你染成黑色的 m 个顶点。
你需要保证:
- 每次操作均合法;
- 同一顶点不会被重复染色(无论在同一操作内还是在不同操作之间);
- 所有操作结束后,图中所有顶点均被染成黑色;
- 你执行的操作次数为 x,其中 x 是所有可行解中最小可能的操作次数。
若存在多个可行解,输出任意一个即可。
输入输出样例
输入#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
输出#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
说明/提示
In the first test case, d1=1 and d2=d3=d4=d5=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.
在第一个测试用例中,d1=1 且 d2=d3=d4=d5=2。我们可以证明:由于不存在两个可同时操作的节点,因此至少需要执行 5 次操作。
在第二个测试用例中,我们可以证明:将整棵树完全染色所需的最少操作次数为 4。示例输出展示了一种在 4 次操作内完成染色的方法。
输入解题思路,AI测评打分。不知道怎么写?