CF1648E.Air Reform

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Berland is a large country with developed airlines. In total, there are nn cities in the country that are historically served by the Berlaflot airline. The airline operates bi-directional flights between mm pairs of cities, ii -th of them connects cities with numbers aia_i and bib_i and has a price cic_i for a flight in both directions.

It is known that Berlaflot flights can be used to get from any city to any other (possibly with transfers), and the cost of any route that consists of several consequent flights is equal to the cost of the most expensive of them. More formally, the cost of the route from a city t1t_1 to a city tkt_k with (k2)(k-2) transfers using cities t2, t3, t4, , tk1t_2,\ t_3,\ t_4,\ \ldots,\ t_{k - 1} is equal to the maximum cost of flights from t1t_1 to t2t_2 , from t2t_2 to t3t_3 , from t3t_3 to t4t_4 and so on until the flight from tk1t_{k - 1} to tkt_k . Of course, all these flights must be operated by Berlaflot.

A new airline, S8 Airlines, has recently started operating in Berland. This airline provides bi-directional flights between all pairs of cities that are not connected by Berlaflot direct flights. Thus, between each pair of cities there is a flight of either Berlaflot or S8 Airlines.

The cost of S8 Airlines flights is calculated as follows: for each pair of cities xx and yy that is connected by a S8 Airlines flight, the cost of this flight is equal to the minimum cost of the route between the cities xx and yy at Berlaflot according to the pricing described earlier.

It is known that with the help of S8 Airlines flights you can get from any city to any other with possible transfers, and, similarly to Berlaflot, the cost of a route between any two cities that consists of several S8 Airlines flights is equal to the cost of the most expensive flight.

Due to the increased competition with S8 Airlines, Berlaflot decided to introduce an air reform and change the costs of its flights. Namely, for the ii -th of its flight between the cities aia_i and bib_i , Berlaflot wants to make the cost of this flight equal to the minimum cost of the route between the cities aia_i and bib_i at S8 Airlines. Help Berlaflot managers calculate new flight costs.

输入格式

Each test consists of multiple test cases. The first line contains one integer tt ( 1t100001 \le t \le 10\,000 ) — the amount of test cases.

The first line of each test case contains two integers nn and mm ( 4n2000004 \le n \le 200\,000 , n1m200000n - 1 \le m \le 200\,000 , m(n1)(n2)2m \le \frac{(n - 1) (n - 2)}{2} ) — the amount of cities in Berland and the amount of Berlaflot flights.

The next mm lines contain the description of Berlaflot flights. The ii -th line contains three integers aia_i , bib_i and cic_i ( 1ai,bin1 \le a_i, b_i \le n , 1ci1091 \le c_i \le 10^9 ) — the numbers of cities that are connected with ii -th Berlaflot flight and the price of ii -th Berlaflot flight.

It is guaranteed that no flight connects a city with itself, no two flights connect the same pair of cities. It is guaranteed that by using Berlaflot flights it is possible to get from any city to any other and by using S8 Airlines flights it is possible to get from any city to any other.

Let NN be the sum of nn over all test cases and MM be the sum of mm over all test cases. It is guaranteed that N,M200000N, M \le 200\,000 .

输出格式

For each test case you should print mm integers in a single line, ii -th of them should be the price of ii -th Berlaflot flight after the air reform.

输入输出样例

  • 输入#1

    3
    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

    输出#1

    3 3 3 
    1 1 1 2 2 
    4 4 5 3 4 4

说明/提示

In the first test case S8 Airlines will provide flights between these pairs of cities: (1,3)(1, 3) , (1,4)(1, 4) and (2,4)(2, 4) .

The cost of a flight between cities 11 and 33 will be equal to 22 , since the minimum cost of the Berlaflot route is 22 — the route consists of a flight between cities 11 and 22 costing 11 and a flight between cities 22 and 33 costing 22 , the maximum cost is 22 .

The cost of a flight between cities 11 and 44 will be 33 , since the minimum cost of the Berlaflot route is 33 — the route consists of a flight between cities 11 and 22 costing 11 , a flight between cities 22 and 33 costing 22 and a flight between cities 33 and 44 costing 33 , the maximum cost is 33 .

The cost of a flight between cities 22 and 44 will be 33 , since the minimum cost of the Berlaflot route is 33 — the route consists of a flight between cities 22 and 33 costing 22 and a flight between cities 33 and 44 costing 33 , the maximum cost is 33 .

After the air reform, the cost of the Berlaflot flight between cities 11 and 22 will be 33 , since the minimum cost of the S8 Airlines route between these cities is 33 — the route consists of a flight between cities 11 and 44 costing 33 and a flight between cities 22 and 44 costing 33 , the maximum cost is 33 .

The cost of the Berlaflot flight between cities 22 and 33 will be 33 , since the minimum cost of the S8 Airlines route between these cities is 33 — the route consists of a flight between cities 22 and 44 costing 33 , a flight between cities 11 and 44 costing 33 and a flight between 11 and 33 costing 22 , the maximum cost is 33 .

The cost of the Berlaflot flight between cities 33 and 44 will be 33 , since the minimum cost of the S8 Airlines route between these cities is 33 — the route consists of a flight between cities 11 and 33 costing 22 and a flight between cities 11 and 44 costing 33 , the maximum cost is 33 .

In the second test case S8 Airlines will have the following flights: between cities 11 and 44 costing 11 , between cities 22 and 33 costing 11 , between cities 22 and 55 costing 22 , between cities 33 and 44 costing 11 and between cities 33 and 55 costing 22 .

首页