CF1592C.Bakry and Partitioning
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Bakry faced a problem, but since he's lazy to solve it, he asks for your help.
You are given a tree of n nodes, the i-th node has value ai assigned to it for each i from 1 to n. As a reminder, a tree on n nodes is a connected graph with n−1 edges.
You want to delete at least 1, but at most k−1 edges from the tree, so that the following condition would hold:
- For every connected component calculate the bitwise XOR of the values of the nodes in it. Then, these values have to be the same for all connected components.
Is it possible to achieve this condition?
巴克里遇到了一个问题,但由于他懒得解决,所以他向你求助。
给你一棵包含 n 个节点的树,对每个 i(从 1 到 n),第 i 个节点被赋予一个值 ai。需要提醒的是:n 个节点上的树是一个具有 n−1 条边的连通图。
你希望从该树中删除至少 1 条、至多 k−1 条边,使得以下条件成立:
- 对每个连通分量,计算其中所有节点的值的按位异或(XOR);然后,所有连通分量所得到的这些 XOR 值必须彼此相等。
能否实现这一条件?
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤5⋅104). Description of the test cases follows.
The first line of each test case contains two integers n and k (2≤k≤n≤105).
The second line of each test case contains n integers a1,a2,...,an (1≤ai≤109).
The i-th of the next n−1 lines contains two integers ui and vi (1≤ui,vi≤n, ui=vi), which means that there's an edge between nodes ui and vi.
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of n over all test cases doesn't exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例数量 t (1≤t≤5⋅104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 k (2≤k≤n≤105)。
每个测试用例的第二行包含 n 个整数 a1,a2,...,an (1≤ai≤109)。
接下来的 n−1 行中,第 i 行包含两个整数 ui 和 vi (1≤ui,vi≤n, ui=vi),表示节点 ui 与 vi 之间存在一条边。
保证所给图是一棵树。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
For each test case, you should output a single string. If you can delete the edges according to the conditions written above, output "YES" (without quotes). Otherwise, output "NO" (without quotes).
You can print each letter of "YES" and "NO" in any case (upper or lower).
对于每个测试用例,你应该输出一个字符串。如果你能按照上述条件删除边,则输出 "YES"(不带引号);否则,输出 "NO"(不带引号)。
你可以以任意大小写(大写或小写)输出 "YES" 和 "NO" 中的每个字母。
输入输出样例
输入#1
5 2 2 1 3 1 2 5 5 3 3 3 3 3 1 2 2 3 1 4 4 5 5 2 1 7 2 3 5 1 2 2 3 1 4 4 5 5 3 1 6 4 1 2 1 2 2 3 1 4 4 5 3 3 1 7 4 1 2 2 3
输出#1
NO YES NO YES NO
说明/提示
It can be shown that the objection is not achievable for first, third, and fifth test cases.
In the second test case, you can just remove all the edges. There will be 5 connected components, each containing only one node with value 3, so the bitwise XORs will be 3 for all of them.
In the fourth test case, this is the tree:
.
You can remove an edge (4,5)
The bitwise XOR of the first component will be, a1⊕a2⊕a3⊕a4=1⊕6⊕4⊕1=2 (where ⊕ denotes the bitwise XOR).
The bitwise XOR of the second component will be, a5=2.
可以证明,该要求在第一、第三和第五个测试用例中无法实现。
在第二个测试用例中,你可以直接删除所有边。此时将得到 5 个连通分量,每个分量仅包含一个权值为 3 的节点,因此所有分量的按位异或(XOR)结果均为 3。
在第四个测试用例中,该树如下所示:
。
你可以删除边 (4,5)。
第一个连通分量的按位异或值为 a1⊕a2⊕a3⊕a4=1⊕6⊕4⊕1=2(其中 ⊕ 表示按位异或运算)。
第二个连通分量的按位异或值为 a5=2。
输入解题思路,AI测评打分。不知道怎么写?