Pangaea 2
Time limit20sMemory limit256 MB
Starting from a given tree, each added road updates the minimum total length that keeps all cities connected, and each test case outputs the XOR of the answers.
- Level
Hard8 of 10
- Topics
- Minimum spanning tree, Tree
- Solved
- No attempts yet
Problem
In the beginning the world had cities and roads joining them. Each city carries an integer label between and . Every road can be travelled in both directions and joins two different cities. In that first world you could walk from any city to every other city along one or more roads.
Humans gained wisdom and built a brilliant civilization, yet laying a new road between two cities was the one thing they never managed.
The creator watched this and began laying one road between two cities every year. The creator also wondered whether humans had the intelligence that the new roads deserve, so the creator posed this puzzle.
Every time a road is laid, choose some of the roads laid so far so that all cities are joined to each other directly or indirectly, and make the total length of the chosen roads as small as possible.
The humans who received the puzzle decided to solve it and show the creator that they deserve to use the roads. Nobody knows what a disappointed creator would do if they failed. You represent humankind, so you have to survive the trial the creator set.
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 laid by the creator , separated by a space ().
The next lines describe the first world. Line () contains two integers (, ) separated by a space, which means city and city are joined by a road of length .
The next lines describe the roads the creator laid, in the order they were laid. Line () contains three integers (, ) separated by spaces, which means the -th road the creator laid joins city and city and has length . and may be equal, and the road may join two cities that a road already joins.
Output
Print one line for each test case. The line contains the XOR of all answers, one answer for each road the creator laid.