Tree and K
Time limit3sMemory limit1024 MB
Given a weighted tree, answer each query with the XOR of all vertex numbers whose distance from vertex x is exactly d.
- Level
Medium7 of 10
- Topics
- Tree, Divide and conquer, Hash map
- Solved
- No attempts yet
Problem
A tree with vertices is given. A tree is a connected graph with no undirected cycles. The vertices are numbered from to , and each edge has a weight. Write a program that processes the following query.
- : Output the XOR of all vertex numbers whose distance from vertex is exactly .
Input
The first line contains and , separated by a space.
From the second line, the next lines each contain two distinct vertex numbers , connected by the -th edge and its weight , separated by spaces.
The input graph is guaranteed to be a tree.
The next lines each contain a query vertex number and a distance , separated by a space, one query per line.
Output
For each query, print the result on its own line, in the order the queries are given.