欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1592C.Bakry and Partitioning
Bakry and Partitioning
Bakry faced a problem, but since he's lazy to solve it, he asks for your help.
You are given a tree of nodes, the -th node has value assigned to it for each from to . As a reminder, a tree on nodes is a connected graph with edges.
You want to delete at least , but at most 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?
Input
Each test contains multiple test cases. The first line contains the number of test cases . Description of the test cases follows.
The first line of each test case contains two integers and .
The second line of each test case contains integers .
The -th of the next lines contains two integers and (, ), which means that there's an edge between nodes and .
It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of over all test cases doesn't exceed .
Output
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).
Note
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 connected components, each containing only one node with value , so the bitwise XORs will be for all of them.
In the fourth test case, this is the tree:
.
You can remove an edge
The bitwise XOR of the first component will be, $a_1 \oplus a_2 \oplus a_3 \oplus a_4 = 1 \oplus 6 \oplus 4 \oplus 1 = 2$ (where denotes the bitwise XOR).
The bitwise XOR of the second component will be, .
Samples
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
NO
YES
NO
YES
NO
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |