Pangaea 1
Time limit20sMemory limit256 MB
After each added road, compute the cheapest total length connecting all cities and XOR the m totals per test case.
- Level
Medium4 of 10
- Topics
- Minimum spanning tree, Union-find, Sorting
- Solved
- No attempts yet
Problem
At the beginning the world had cities and roads joining them. The cities carry the integer numbers through . Every road can be walked in both directions and joins two different cities. Starting from any city you could reach every other city by walking along one or more roads.
Humans grew wise and built a bright civilization, but laying a new road between two cities was the one thing they never managed.
The creator watched this and started adding one road between two cities every year. He also wanted to know whether humans were clever enough to use the new roads, so he set them the following puzzle.
Every time one road is added, choose some of the roads laid so far so that all cities are connected to each other, directly or indirectly. Make the sum of the lengths of the chosen roads as small as possible, and report that minimum sum.
You represent humankind, and you have to solve the puzzle.
Input
The first line contains the number of test cases .
The first line of each test case contains the number of cities and the number of roads the creator laid , separated by a space.
The next lines describe the world at the beginning. Line () among them contains the integers , (, ), separated by a space. This means that city and city are joined by a road of length .
The next lines give the roads the creator laid, in the order he laid them. Line () among them contains the integers , , (, ), separated by a space. This means that the -th road joins city and city and has length . and can be equal, and the road can join two cities that a road already joins.
The limits are , , and .
Output
Print one line for each test case. On that line print the XOR of all minimum sums, one for each added road.