#C10762. Minimum Spanning Tree Calculation

    ID: 40003 Type: Default 1000ms 256MiB

Minimum Spanning Tree Calculation

Minimum Spanning Tree Calculation

You are given an undirected graph with n vertices and m edges. Each edge is described by three integers u, v and w representing an edge between vertices u and v with weight w. Your task is to compute the weight of the minimum spanning tree (MST) using Kruskal's algorithm. In a connected graph, the total weight of the MST is given by \(\sum_{e \in MST} w_e\). If the graph is disconnected, output DISCONNECTED.

Note: The vertices are numbered from 1 to n. Use an efficient union-find (disjoint set union) data structure to handle the connectivity.

inputFormat

The input is given via standard input (stdin) in the following format:

  • The first line contains two integers n and m, the number of vertices and edges respectively.
  • Each of the next m lines contains three space-separated integers u, v, and w representing an edge between vertices u and v with weight w.

outputFormat

Output a single line to standard output (stdout):

  • If the graph is connected, print the total weight of the MST.
  • If the graph is disconnected, print DISCONNECTED (without quotes).
## sample
4 5
1 2 1
1 3 4
2 3 2
2 4 3
3 4 5
6