Air Reform
Time limit2sMemory limit1024 MB
Given a connected weighted graph, find for each edge the cheapest route between its endpoints in the complement graph, where a route costs its largest edge.
- Level
Hard8 of 10
- Topics
- Graph, Minimum spanning tree, BFS
- Solved
- No attempts yet
Statement
Berland is a large country with developed airlines. It has cities, and the airline Berlaflot operates bidirectional flights between pairs of cities. The -th flight connects cities and and costs in both directions. Berlaflot flights can be used to get from any city to any other, possibly with transfers. The cost of a route made of several consecutive flights is the maximum cost among those flights. More formally, the cost of a route from to through the cities is the maximum cost of the flights from to , from to , and so on until the flight from to . All flights on the route must be operated by Berlaflot.
Input
Each test consists of multiple test cases. The first line contains two integers and (, ), the number of test cases and the number of the group this test belongs to. Each test case starts with a line containing and (, , ), the number of cities and the number of Berlaflot flights. Then lines follow. The -th line contains , , and (, ). No flight connects a city to itself, and no two flights connect the same pair of cities. Berlaflot flights can reach any city from any other city, and S8 Airlines flights can too. Let be the sum of and be the sum of over all test cases. Then .
Output
For each test case, print integers on a single line. The -th integer is the new price of the -th Berlaflot flight.
Hint
In the first test case, S8 Airlines provides flights between cities 1 and 3, 1 and 4, and 2 and 4. The cost of the flight between 1 and 3 is 2, because the cheapest Berlaflot route goes from 1 to 2 (cost 1) and then from 2 to 3 (cost 2), and its maximum is 2. The cost between 1 and 4 is 3, because the route 1 to 2 to 3 to 4 has costs 1, 2, 3 and maximum 3. The cost between 2 and 4 is 3, because the route 2 to 3 to 4 has maximum 3.
After the reform, the cost of the Berlaflot flight between 1 and 2 is 3, because the S8 route 1 to 4 to 2 has costs 3 and 3, so its maximum is 3. The cost of the flight between 2 and 3 is 3, because the S8 route 2 to 4 to 1 to 3 has costs 3, 3, 2, and its maximum is 3. The cost of the flight between 3 and 4 is 3, because the S8 route 3 to 1 to 4 has costs 2 and 3, and its maximum is 3.
In the second test case, S8 Airlines has flights between 1 and 4 costing 1, 2 and 3 costing 1, 2 and 5 costing 2, 3 and 4 costing 1, and 3 and 5 costing 2.