Spanning Tree Range

View as PDF

Submit solution

Points: 10
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

Given a bidirectional, connected, weighted graph with N nodes and M edges, find the difference between the sum of the edges of the maximum and minimum spanning trees of the graph.

Constraints

1 \le N, M \le 2 \times 10^5

Input Specification

The first line of input will contain 2 space-separated integers N and M.

The next M lines will contain 3 space-separated integers u, v, and w \ (1 \le u, v \le N, 1 \le w \le 10^9) denoting an edge.

Output Specification

Output one integer, the difference between the sum of the edges of the maximum and minimum spanning trees.

Note: 64-bit integers may be required for this problem.

Sample Input

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

Sample Output

5

The graph given is shown in the above figure. The sum of the edges of the minimum spanning tree is 6 using edges 1, 2, and 5 (in the sample input). The sum of the edges of the maximum spanning tree is 11, using edges 2, 3, and 4. The difference is thus 5.


Comments

There are no comments at the moment.