Royal Problem
Time limit3sMemory limit1024 MB
Given a directed graph with XOR-weighted edges, find the XOR of the XORs of all walks from a to b, or report -1 if infinitely many walks have nonzero XOR.
- Level
Hard8 of 10
- Topics
- Graph, Bit manipulation, Math, Union-find
- Solved
- No attempts yet
Problem
Not long ago, a new road network was built in Berland. Some pairs of cities are connected by one-way roads. The -th road goes from city to city and has length . The two main cities of Berland are numbered and .
The king of Berland loves his country and likes to compute all kinds of its characteristics. He calls the beauty of a path the bitwise XOR of the lengths of all roads on that path. He calls the beauty of his country the bitwise XOR of the beauties of all paths from city to city . Note that there may be infinitely many such paths, and they may pass through the same city several times.
The king wants to know the beauty of his country. Compute it, or report that it cannot be computed.
The bitwise XOR of a set of numbers is the bitwise XOR of all nonzero numbers in the set. If the set contains infinitely many nonzero numbers, its bitwise XOR cannot be computed.
The bitwise XOR (bitwise addition modulo two) is a binary operation that applies logical XOR to each pair of bits at the same position. If the corresponding bits differ, the result bit is 1. If they match, the result bit is 0. For example, for and , we get .
A path in a graph is a sequence of vertices in which every two consecutive vertices are connected by an edge.
Input
The first line contains one integer (), the number of test cases.
The first line of each test case contains two integers and (), the number of cities and the number of roads.
Each of the next lines contains three integers , , and (, ), the start city, end city, and length of the -th road.
The last line of each test case contains two integers and (), the start and end cities of the paths the king is interested in.
The sum of over all test cases does not exceed , and the sum of over all test cases does not exceed .
Output
For each test case, print one integer, the beauty of Berland. If the answer does not exist, print .
Hints
In the first test case, the country has only one road, of length , so the beauty of any path is . Therefore the bitwise XOR of all path beauties is .
In the second test case, there are 6 paths from city 1 to city 3, with beauties , , , , , and . The beauty of the country is .
In the third test case, paths from city 1 to city 2 have beauties , , , , and so on. There are infinitely many paths with nonzero beauty, so the answer cannot be computed.
In the fourth test case, there are infinitely many paths from city 2 to city 3 with beauty , and no path with nonzero beauty. So the beauty of the country is .