Cactus
Time limit2sMemory limit512 MB
Given a cactus graph with XOR edge costs, answer each query with the k-th smallest distinct XOR value over all simple paths between two vertices.
- Level
Hard9 of 10
- Topics
- Graph, DFS, Bit manipulation, Tree
- Solved
- No attempts yet
Problem
A cactus graph is a connected undirected graph with no self-loops or multiple edges in which each edge belongs to at most one simple cycle.
You are given a cactus graph with vertices, numbered from to , and edges. The -th edge connects vertices and , and its cost is .
The cost of a simple path in is the bitwise XOR of the costs of all edges on that path.
Answer queries of the form " ". Consider the costs of all simple paths connecting vertices and , remove duplicate values, sort them in ascending order, and output the -th element. If there are fewer than distinct values, output .
Input
The first line contains two integers and , the number of vertices and edges (, ).
Each of the next lines contains three integers , , and (, , ).
The next line contains an integer , the number of queries ().
Each of the next lines contains three integers , , and (, , ).
Output
For each query, print one integer on its own line: the answer to that query.