Kleptocrat
Time limit7sMemory limit512 MB
Given a connected weighted graph where path length is the XOR of edge weights, answer queries for the shortest distance between two nodes.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Bit manipulation, DFS
- Solved
- No attempts yet
Problem
Your company has a policy that refunds every employee an amount proportional to the shortest distance between their home and their office. This creates the loophole that many employees deliberately move very far away to claim the largest possible reimbursement.
One employee has abused this policy far too much and is about to bankrupt you. You have to stop this before you cancel the policy next year. The rules are strict, though: as long as the employee keeps track of the distances they have travelled, you are forced to reimburse them.
Then you have a flash of inspiration. Nowhere does it say you have to use Euclidean distances! You start working on subtler distance functions, and now you have a first prototype: XOR distance. The length of a path is the XOR of the lengths of the edges on the path, not the sum. The distance between two locations is the length of the shortest path between them.
You want to test this principle on the transport network, for the location of each of your employees in turn.
Input
- The first line contains three integers (), (), and (): the number of nodes, edges, and questions.
- The next lines describe the edges. Each line contains three integers , , (, , ), meaning there is an undirected edge of length between nodes and .
- The next lines describe the questions. Each line contains two integers , (), asking for the shortest distance between nodes and .
Between every pair of distinct nodes there is at most one edge, and every node is reachable from every other node.
Output
For each question, output the shortest distance between nodes and on its own line.