#P8201. [传智杯 #4 决赛] [yLOI2021] 生活在树上(hard version)
[传智杯 #4 决赛] [yLOI2021] 生活在树上(hard version)
Background
This problem is the harder version of P8200, and the solutions to the two problems are slightly different. The difference in the statement between this problem and P8200 is that this problem gives node weights on the tree, rather than edge weights.
Xiao Zhi lives in “ChuanZhi Kingdom”, a country with cities, where the cities are connected by roads.
Each city has a wealth index . We define the cost for Xiao Zhi to travel from city to city as $\mathrm{dis}_{a, b} = \bigoplus \limits_{u \in \mathrm{path}\left(a, b\right)} w_u$, where denotes bitwise XOR (if you do not know what bitwise XOR is, please refer to the hints/explanations below), and denotes the set of nodes on the simple path from to (including and ). That is, means: after listing all nodes on the simple path from to as , compute .
One day, Xiao Zhi got the chance to participate in the ChuanZhi Cup. On his way to the contest venue, he thought of a problem, but it seems he cannot solve it, so he told you this problem. Smart classmate, can you help him?
Problem Description
Xiao Zhi said: “Since our country has only cities and roads, then our country is equivalent to a tree. I am thinking: in our country, does there exist a city such that ‘the XOR of the cost to city and the cost to city equals ’? It is so hard, I cannot figure it out. Can you help me?”
That is, given cities and an integer , please determine whether there exists a city such that $\mathrm{dis}_{t, a} \bigoplus \mathrm{dis}_{t, b} = k$.
Input Format
The first line contains two integers , , denoting the number of cities and the number of queries.
The second line contains integers , denoting the wealth index of city .
The next lines each contain two integers , indicating that there is an edge connecting city and city .
The next lines each contain three integers , indicating that Xiao Zhi travels from city to city , and the meaning of is the same as in the description.
Output Format
Output a total of lines.
For the -th query, if there exists at least one city that satisfies the requirement, output Yes.
If there is no city that satisfies the condition, output No.
The output is case-insensitive. For example, Yes, yES, YES, yes, etc. are all accepted as Yes.
5 3
2 6 8 1 5
1 2
1 3
2 4
2 5
1 2 4
2 3 12
2 3 10
nO
No
YeS
5 10
93 97 100 93 93
2 1
3 2
4 3
5 1
5 2 93
4 1 93
3 2 100
3 2 100
2 3 9999998
1 2 93
2 3 97
1 2 93
2 3 97
4 3 93
no
nO
yEs
yEs
No
yEs
yeS
YES
yES
yes
Hint
Explanation of related concepts
“Tree”: A tree is an undirected simple connected graph with nodes and edges.
“Bitwise XOR”: Bitwise XOR is a binary operation. Compare the binary bits of two numbers bit by bit: the same gives , different gives . For example, $3 \bigoplus 5 = (011)_2 \bigoplus (101)_2 = (110)_2 = 6$.
Explanation of Sample 1
The figure below shows the map of ChuanZhi Kingdom.
, it is impossible to have $\mathrm{dis} _{t,1} \bigoplus \mathrm{dis}_{t, 2} = 4$ and $\mathrm{dis}_{t, 2} \bigoplus \mathrm{dis}_{t, 3} = 12$, so output No.
If we take , then $\mathrm{dis}_{t, 2} \bigoplus \mathrm{dis}_{t, 3} = 10$, so output Yes.

Constraints
For all test points, it is guaranteed that , , .
For each query, it is guaranteed that and , .
Notes
- Please pay attention to the impact of constant factors on program efficiency.
- For two numbers , may be greater than . Please pay special attention to this.
Translated by ChatGPT 5