XOR, Sets, Trees, and Queries
Time limit2sMemory limit1024 MB
Given a tree with a value on each vertex, find for each query path the minimum element of the closure of its path values under XOR, doubling, and halving.
- Level
Medium7 of 10
- Topics
- Bit manipulation, Tree, DFS
- Solved
- No attempts yet
Problem
For a finite set of positive integers, is defined as the smallest finite set that satisfies the following conditions. Exactly one such set exists.
- .
- For any positive integers and , if and , then . Here is the bitwise XOR.
- For any positive integer , if and , then .
- For any positive integer , if and , then .
A tree has vertices. For every (), vertex has a positive integer written on it. You must process queries.
For a query , let be the set of vertices on the shortest path from vertex to vertex . Output the minimum element of the set .
Input
The first line contains the number of vertices and the number of queries , separated by a space.
The second line contains the positive integers written on the vertices, separated by spaces.
From the third line, the next lines each contain two vertex numbers and , separated by a space, that are connected by an edge of the tree.
From line , the next lines each contain two integers and , separated by a space, that describe a query.
Output
For each query, print the answer on its own line. Print lines in total.
Constraints
- ()
- In every query,