#P2420. 让我们异或吧

    ID: 1398 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>树形结构洛谷原创深度优先搜索,DFS最近公共祖先,LCA树链剖分,树剖

让我们异或吧

Description

XOR is a special operation, often summarized as addition without carry.

In daily life, xor is also common. For example, for a yes/no question, yes is 11 and no is 00, then: (A is male) xor (B is male) = whether A and B can become a couple.

Now, we will handle a more complex case. You are given a tree with NN nodes. Each edge has a weight. There are MM queries. For each query, find the XOR of all edge weights along the path between two nodes.

Input Format

The first line contains an integer NN, the number of nodes in the tree. The next N1N - 1 lines each contain three integers u,v,wu, v, w, indicating there is an edge between uu and vv with weight ww. The next line contains an integer MM, the number of queries. Each of the next MM lines contains two integers u,vu, v, asking for the XOR of the edge weights along the path between uu and vv.

Output Format

Output MM lines, each with one integer, the XOR value.

5
1 4 9644
2 5 15004
3 1 14635
5 3 9684
3
2 4
5 4
1 1

975
14675
0

Hint

Constraints:

  • For 40%40\% of the testdata, 1N,M30001 \le N, M \le 3000.
  • For 100%100\% of the testdata, 1N,M1000001 \le N, M \le 100000.
  • Edge weights are within the int range.

Translated by ChatGPT 5