#C8834. Minimal Bridge Length

    ID: 52860 Type: Default 1000ms 256MiB

Minimal Bridge Length

Minimal Bridge Length

In a faraway kingdom, there are n islands connected by m bidirectional bridges. Each bridge has a positive length, and the King wishes to ensure that every island is connected (directly or indirectly) while minimizing the total length of the bridges used.

You are given the number of islands and the details of each bridge. Your task is to compute the minimal total length of bridges required to connect all islands. If it is impossible to connect all islands using the available bridges, output \(\text{IMPOSSIBLE}\).

Hint: This problem can be solved using a Minimum Spanning Tree algorithm, such as Kruskal's algorithm.

inputFormat

The first line of input contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105), representing the number of islands and the number of bridges, respectively.

The next m lines each contain three integers u, v, and w (1 ≤ u, v ≤ n and 1 ≤ w ≤ 106), which describe a bidirectional bridge between islands u and v with length w.

outputFormat

Output the minimum total length required to connect all islands. If it is not possible to connect every island, output \(\text{IMPOSSIBLE}\).

## sample
4 5
1 2 1
1 3 4
2 3 2
3 4 3
4 1 5
6