#P8200. [传智杯 #4 决赛] 生活在树上(easy version)

    ID: 9255 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>树形数据结构Special JudgeO2优化深度优先搜索 DFS前缀和传智杯

[传智杯 #4 决赛] 生活在树上(easy version)

Background

This problem is the easy version of P8201, and the solutions to the two problems are slightly different. The difference in the statement between this problem and P8201 is that this problem gives edge weights on the tree, not node weights.

Xiaozhi lives in “Chuanzhi Kingdom”, a country with nn cities, where the cities are connected by n1n - 1 roads.

Each road has a length wiw_i. We define the cost for Xiaozhi to walk from city aa to city bb as $\mathrm{dis}_{a, b} = \bigoplus \limits_{e \in \mathrm{path}\left(a, b\right)} w_e$, where \bigoplus denotes bitwise XOR (if you do not know what bitwise XOR is, please refer to the hint/explanation below), and path(a,b)\mathrm{path}\left(a,b\right) denotes the set of edges on the simple path from aa to bb. In other words, disa,b\mathrm{dis}_{a, b} is the result of taking all edges on the simple path between aa and bb, written as e1,e2,e3,e_1, e_2, e_3, \dots, and computing we1we2we3w_{e_1} \bigoplus w_{e_2} \bigoplus w_{e_3} \dots.

One day, Xiaozhi got a 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 about it. Smart classmate, can you help him?

Problem Description

Xiaozhi said: “Since our country has only nn cities and n1n - 1 roads, then our country is equivalent to a tree. I was thinking: in our country, is there a city such that ‘the XOR of the cost to city aa and the cost to city bb equals kk’? This is so hard. I cannot figure it out. Can you help me?”

That is, given cities a,ba, b and an integer kk, please determine whether there exists a city tt such that $\mathrm{dis}_{t, a} \bigoplus \mathrm{dis}_{t, b} = k$.

Input Format

The first line contains two integers nn, mm, representing the number of cities in the country and the number of queries.

The next n1n - 1 lines each contain three integers x,y,lx, y, l, indicating that there is an edge of length ll between city xx and city yy.

The next mm lines each contain three integers a,b,ka, b, k, indicating Xiaozhi walks from city aa to city bb, and the meaning of kk is the same as in the problem description.

Output Format

Output mm lines, each containing one string.

For the ii-th query, if there exists at least one city tt that satisfies the requirement, output Yes.

If there is no city that satisfies the requirement, output No.

The output is case-insensitive. For example, Yes, yES, YES, yes, etc. are all accepted as Yes.

5 3
1 2 2
1 3 6 
2 4 8
2 5 1
1 2 4
2 3 12
1 4 10
nO
No
YeS
5 10
2 1 63
3 1 57
4 2 2
5 2 84
5 2 84
4 1 9977404983223574764
2 5 84
2 1 15996060349666123522
5 4 86
3 1 8428615422876116375
5 1 107
2 3 6
2 3 6
4 2 2
yeS
nO
YEs
No
YEs
nO
YEs
yeS
yeS
YEs

Hint

“Tree”: A tree is an undirected simple connected graph with nn nodes and n1n - 1 edges.

“Bitwise XOR”: Bitwise XOR is the ^ operator in C++, python, and java. It is a binary operation. Compare the binary bits of two numbers bit by bit: if they are the same, the result bit is 00; if they are different, the result bit is 11. For example: $3 \bigoplus 5 = (011)_2 \bigoplus (101)_2 = (110)_2 = 6$. Note that this is a bitwise operation, not a logical operation.

Explanation for Sample 1

The figure below shows the map of Chuanzhi Kingdom.

For all t{1,2,3,4,5}t \in \{1, 2, 3, 4, 5\}, 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 the output is No.

If we take t=5t = 5, we have $\mathrm{dis}_{t, 1} \bigoplus \mathrm{dis}_{t, 4} = 10$, so the output is Yes.

Constraints

For all test points, it is guaranteed that 1<n5×1051 < n \leq 5 \times 10^5, 1m5×1051 \leq m \leq 5 \times 10^5, 0wi<2640 \leq w_i < 2^{64}.

For each query, it is guaranteed that 1a,bn1 \leq a, b \leq n and aba \neq b, 0k<2640 \leq k < 2^{64}.

Translated by ChatGPT 5