#C957. Minimum Cable Upgrade

    ID: 53677 Type: Default 1000ms 256MiB

Minimum Cable Upgrade

Minimum Cable Upgrade

You are given a network of n servers and m cables connecting them. Each cable has an associated capacity. Your task is to determine the minimum capacity cable among those used in the Minimum Spanning Tree (MST) of the network, which connects all the servers. If the network is disconnected such that an MST cannot be formed, output -1.

Formally, you are given an undirected graph with vertices numbered from 1 to n and m edges. Each edge is described by three integers u, v, and w, where w represents the capacity of the cable connecting servers u and v. You are to construct an MST using, for example, Kruskal's algorithm. After forming a spanning tree, determine the minimum cable capacity (i.e. the smallest edge weight in the MST) and output it.

If it is impossible to span all servers (i.e. the graph is disconnected), output -1.

Note: All formulas are in LaTeX format. For instance, an MST spanning tree has n - 1 edges, and if no such tree exists, output \(-1\).

inputFormat

The input is read from standard input (stdin). The first line contains two integers n and m where n is the number of servers and m is the number of cables. The following m lines each contain three integers: u, v, and w, representing a cable between servers u and v with capacity w.

outputFormat

Output the minimum cable capacity (the minimum edge weight in the MST) on a single line. If it is impossible to connect all servers, output -1.

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