题库练习 XOR, Tree, and Queries
← 上一题 下一题 →

A15723 | XOR, Tree, and Queries

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

You are given a tree of $n$ vertices. The vertices are numbered from $1$ to $n$ .

You will need to assign a weight to each edge. Let the weight of the $i$ -th edge be $a_i$ ( $1 \leq i \leq n-1$ ). The weight of each edge should be an integer between $0$ and $2^{30}-1$ , inclusive.

You are given $q$ conditions. Each condition consists of three integers $u$ , $v$ , and $x$ . This means that the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of all edges on the shortest path from $u$ to $v$ should be $x$ .

Find out if there exist $a_1, a_2, \ldots, a_{n-1}$ that satisfy the given conditions. If yes, print a solution such that $a_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 $a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1}$ is the smallest, print any.

输入格式

The first line contains two integers $n$ and $q$ ( $2 \le n \le 2.5 \cdot 10^5$ , $0 \le q \le 2.5 \cdot 10^5$ ).

The $i$ -th of the following $n-1$ lines contains two integers $x_i$ and $y_i$ ( $1 \le x_i, y_i \le n$ , $x_i \neq y_i$ ), meaning that the $i$ -th edge connects vertices $x_i$ and $y_i$ in the tree.

It is guaranteed that the given edges form a tree.

The following $q$ lines contain information about conditions.

Each line contains three integers $u$ , $v$ , $x$ ( $1 \le u, v \le n$ , $u \neq v$ , $0 \le x \le 2^{30}-1$ ), meaning that the bitwise XOR of all edges on the shortest path from $u$ to $v$ should be $x$ .

输出格式

If there do not exist $a_1$ , $a_2$ , ..., $a_{n-1}$ that satisfy the given conditions, print "No".

Otherwise, print "Yes" in the first line.

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

If there are multiple solutions such that $a_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.

输入输出样例

输入 #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
C++ 编辑器
输入
输出