CF2183D1.Tree Coloring (Easy Version)
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the Easy version of the problem. The difference between the versions is that in this version, you are only required to find the minimum number of 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.
∗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 the minimum number of operations on a new line.
对于每个测试用例,在新的一行输出所需的最少操作次数。
输入输出样例
输入#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 4 4 3 3 3 4 4 4 3
说明/提示
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. One way to do so is to color nodes 1 and 3 in the same operation, and all 3 other nodes in their own operations.
在第一个测试用例中,d1=1 且 d2=d3=d4=d5=2。我们可以证明:至少需要执行 5 次操作,因为不存在两个可以同时操作的节点。
在第二个测试用例中,我们可以证明:为整棵树染色所需的最少操作次数为 4。其中一种方案是:在同一次操作中对节点 1 和 3 进行染色,其余 3 个节点各自在单独的操作中染色。
输入解题思路,AI测评打分。不知道怎么写?