CF1788F.XOR, Tree, and Queries

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree of nn vertices. The vertices are numbered from 11 to nn.

You will need to assign a weight to each edge. Let the weight of the ii-th edge be aia_i (1≤i≤n−11 \leq i \leq n-1). The weight of each edge should be an integer between 00 and 230−12^{30}-1, inclusive.

You are given qq conditions. Each condition consists of three integers uu, vv, and xx. This means that the bitwise XOR of all edges on the shortest path from uu to vv should be xx.

Find out if there exist a1,a2,…,an−1a_1, a_2, \ldots, a_{n-1} that satisfy the given conditions. If yes, print a solution such that a1⊕a2⊕…⊕an−1a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} is the smallest. Here, ⊕\oplus denotes the bitwise XOR operation.

If there are multiple solutions such that a1⊕a2⊕…⊕an−1a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} is the smallest, print any.

给你一棵包含 nn 个顶点的树,顶点编号为 11 到 nn。

你需要为每条边分配一个权值。设第 ii 条边的权值为 aia_i(1≤i≤n−11 \leq i \leq n-1)。每条边的权值必须是介于 00 和 230−12^{30}-1(含)之间的整数。

你将获得 qq 个约束条件。每个条件由三个整数 uu、vv 和 xx 组成,表示从顶点 uu 到顶点 vv 的最短路径上所有边的权值的按位异或(bitwise XOR)结果应恰好为 xx。

请判断是否存在满足所有约束条件的权值序列 a1,a2,…,an−1a_1, a_2, \ldots, a_{n-1}。若存在,请输出一个解,使得 a1⊕a2⊕…⊕an−1a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} 的值最小(其中 ⊕\oplus 表示按位异或运算)。

若存在多个使 a1⊕a2⊕…⊕an−1a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} 最小的解,输出任意一个即可。

输入格式

The first line contains two integers nn and qq (2≤n≤2.5⋅1052 \le n \le 2.5 \cdot 10^5, 0≤q≤2.5⋅1050 \le q \le 2.5 \cdot 10^5).

The ii-th of the following n−1n-1 lines contains two integers xix_i and yiy_i (1≤xi,yi≤n1 \le x_i, y_i \le n, xi≠yix_i \neq y_i), meaning that the ii-th edge connects vertices xix_i and yiy_i in the tree.

It is guaranteed that the given edges form a tree.

The following qq lines contain information about conditions.

Each line contains three integers uu, vv, xx (1≤u,v≤n1 \le u, v \le n, u≠vu \neq v, 0≤x≤230−10 \le x \le 2^{30}-1), meaning that the bitwise XOR of all edges on the shortest path from uu to vv should be xx.

第一行包含两个整数 nn 和 qq(2≤n≤2.5⋅1052 \le n \le 2.5 \cdot 10^5,0≤q≤2.5⋅1050 \le q \le 2.5 \cdot 10^5)。

接下来的 n−1n-1 行中,第 ii 行包含两个整数 xix_i 和 yiy_i(1≤xi,yi≤n1 \le x_i, y_i \le n,xi≠yix_i \neq y_i),表示第 ii 条边连接树中的顶点 xix_i 和 yiy_i。

保证所给的边构成一棵树。

接下来的 qq 行描述约束条件。

每行包含三个整数 uu、vv、xx(1≤u,v≤n1 \le u, v \le n,u≠vu \neq v,0≤x≤230−10 \le x \le 2^{30}-1),表示从 uu 到 vv 的最短路径上所有边的权值的按位异或(XOR)结果应为 xx。

输出格式

If there do not exist a1a_1, a2a_2, ..., an−1a_{n-1} that satisfy the given conditions, print "No".

Otherwise, print "Yes" in the first line.

Then print n−1n-1 integers on the next line, where the ii-th integer is the weight of the ii-th edge. If there are multiple solutions that satisfy the given conditions, print a solution such that a1⊕a2⊕…⊕an−1a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} is the smallest.

If there are multiple solutions such that a1⊕a2⊕…⊕an−1a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} is the smallest, print any.

When printing "Yes" or "No", you can print each letter in any case (either upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

如果不存在满足给定条件的 a1a_1, a2a_2, ..., an−1a_{n-1},则输出 "No"。

否则,在第一行输出 "Yes"。

然后在下一行输出 n−1n-1 个整数,其中第 ii 个整数为第 ii 条边的权重。若存在多个满足给定条件的解,则输出使得 a1⊕a2⊕…⊕an−1a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} 最小的解。

若存在多个使得 a1⊕a2⊕…⊕an−1a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} 最小的解,则任选其一输出。

在输出 "Yes" 或 "No" 时,每个字母可使用任意大小写(大写或小写均可)。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均将被识别为肯定回答。

输入输出样例

  • 输入#1

    4 4
    1 2
    2 3
    3 4
    1 4 3
    2 4 2
    1 3 1
    2 3 1

    输出#1

    No
  • 输入#2

    6 2
    1 2
    2 3
    3 4
    2 5
    5 6
    1 4 2
    2 6 7

    输出#2

    Yes
    4 2 4 1 6
  • 输入#3

    6 2
    1 2
    2 3
    3 4
    2 5
    5 6
    1 4 3
    1 6 5

    输出#3

    Yes
    6 1 4 3 0

说明/提示

For the first example, there does not exist a set of edge weights that satisfies the given conditions.

For the second example, the two given conditions are a1⊕a2⊕a3=2a_1 \oplus a_2 \oplus a_3=2 and a4⊕a5=7a_4 \oplus a_5=7. There can be multiple solutions, for example, (a1,a2,a3,a4,a5)=(1,2,1,4,3)(a_1, a_2, a_3, a_4, a_5)=(1, 2, 1, 4, 3).

For the third example, the two given conditions are a1⊕a2⊕a3=3a_1 \oplus a_2 \oplus a_3=3 and a1⊕a4⊕a5=5a_1 \oplus a_4 \oplus a_5=5. There are multiple solutions that satisfy the given conditions.

(a1,a2,a3,a4,a5)=(1,1,3,4,0)(a_1, a_2, a_3, a_4, a_5)=(1, 1, 3, 4, 0) satisfies the given conditions, but the bitwise XOR of all edge weights is 77, which does not have the smallest a1⊕a2⊕…⊕an−1a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1}, so it cannot be the answer.

对于第一个样例,不存在满足给定条件的边权集合。

对于第二个样例,给出的两个条件是 a1⊕a2⊕a3=2a_1 \oplus a_2 \oplus a_3=2 和 a4⊕a5=7a_4 \oplus a_5=7。存在多种解,例如 (a1,a2,a3,a4,a5)=(1,2,1,4,3)(a_1, a_2, a_3, a_4, a_5)=(1, 2, 1, 4, 3)。

对于第三个样例,给出的两个条件是 a1⊕a2⊕a3=3a_1 \oplus a_2 \oplus a_3=3 和 a1⊕a4⊕a5=5a_1 \oplus a_4 \oplus a_5=5。存在多个满足给定条件的解。

(a1,a2,a3,a4,a5)=(1,1,3,4,0)(a_1, a_2, a_3, a_4, a_5)=(1, 1, 3, 4, 0) 满足给定条件,但所有边权的按位异或值为 77,该值并非最小的 a1⊕a2⊕…⊕an−1a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1},因此不能作为答案。

输入解题思路,AI测评打分。不知道怎么写?

首页