A14620 | Bakry and Partitioning
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $a_i$ 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](https://en.wikipedia.org/wiki/Bitwise_operation#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?
You are given a tree of $n$ nodes, the $i$ -th node has value $a_i$ 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](https://en.wikipedia.org/wiki/Bitwise_operation#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?
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $t$ $(1 \leq t \leq 5 \cdot 10^4)$ . Description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ $(2 \leq k \leq n \leq 10^5)$ .
The second line of each test case contains $n$ integers $a_1, a_2, ..., a_n$ $(1 \leq a_i \leq 10^9)$ .
The $i$ -th of the next $n-1$ lines contains two integers $u_i$ and $v_i$ ( $1 \leq u_i, v_i \leq n$ , $u_i\neq v_i$ ), which means that there's an edge between nodes $u_i$ and $v_i$ .
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 \cdot 10^5$ .
The first line of each test case contains two integers $n$ and $k$ $(2 \leq k \leq n \leq 10^5)$ .
The second line of each test case contains $n$ integers $a_1, a_2, ..., a_n$ $(1 \leq a_i \leq 10^9)$ .
The $i$ -th of the next $n-1$ lines contains two integers $u_i$ and $v_i$ ( $1 \leq u_i, v_i \leq n$ , $u_i\neq v_i$ ), which means that there's an edge between nodes $u_i$ and $v_i$ .
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 \cdot 10^5$ .
输出格式
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).
You can print each letter of "YES" and "NO" in any case (upper or lower).
输入输出样例
输入 #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, $a_1 \oplus a_2 \oplus a_3 \oplus a_4 = 1 \oplus 6 \oplus 4 \oplus 1 = 2$ (where $\oplus$ denotes the bitwise XOR).
The bitwise XOR of the second component will be, $a_5 = 2$ .
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, $a_1 \oplus a_2 \oplus a_3 \oplus a_4 = 1 \oplus 6 \oplus 4 \oplus 1 = 2$ (where $\oplus$ denotes the bitwise XOR).
The bitwise XOR of the second component will be, $a_5 = 2$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted