Under construction

Still being built. Things may change

Air Reform

Time limit2sMemory limit1024 MB

Summary
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 nn cities, and the airline Berlaflot operates bidirectional flights between mm pairs of cities. The ii-th flight connects cities aia_i and bib_i and costs cic_i 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 t1t_1 to tkt_k through the cities t2,t3,…,tk−1t_2, t_3, \ldots, t_{k-1} is the maximum cost of the flights from t1t_1 to t2t_2, from t2t_2 to t3t_3, and so on until the flight from tk−1t_{k-1} to tkt_k. All flights on the route must be operated by Berlaflot.

Input

Each test consists of multiple test cases. The first line contains two integers tt and gg (1≤t≤100001 \le t \le 10000, 0≤g≤80 \le g \le 8), the number of test cases and the number of the group this test belongs to. Each test case starts with a line containing nn and mm (4≤n≤2000004 \le n \le 200000, n−1≤m≤200000n - 1 \le m \le 200000, m≤(n−1)(n−2)2m \le \frac{(n - 1)(n - 2)}{2}), the number of cities and the number of Berlaflot flights. Then mm lines follow. The ii-th line contains aia_i, bib_i, and cic_i (1≤ai,bi≤n1 \le a_i, b_i \le n, 1≤ci≤1091 \le c_i \le 10^9). 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 NN be the sum of nn and MM be the sum of mm over all test cases. Then N,M≤200000N, M \le 200000.

Output

For each test case, print mm integers on a single line. The ii-th integer is the new price of the ii-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.

Examples1

  1. Example 1

    Input
    3 0
    4 3
    1 2 1
    2 3 2
    4 3 3
    5 5
    1 2 1
    1 3 1
    2 4 1
    4 5 2
    5 1 3
    6 6
    1 2 3
    2 3 1
    3 6 5
    3 4 2
    4 5 4
    2 4 2
    
    Expected output
    3 3 3
    1 1 1 2 2
    4 4 5 3 4 4