2018-10-19 12:48:28 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2019-07-17 04:07:25 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
num_nodes, num_edges = list(map(int, input().strip().split()))
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-07-17 04:07:25 +00:00
|
|
|
edges = []
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-07-17 04:07:25 +00:00
|
|
|
for i in range(num_edges):
|
|
|
|
node1, node2, cost = list(map(int, input().strip().split()))
|
|
|
|
edges.append((i,node1,node2,cost))
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-07-17 04:07:25 +00:00
|
|
|
edges = sorted(edges, key=lambda edge: edge[3])
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-07-17 04:07:25 +00:00
|
|
|
parent = list(range(num_nodes))
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-07-17 04:07:25 +00:00
|
|
|
def find_parent(i):
|
|
|
|
if i != parent[i]:
|
|
|
|
parent[i] = find_parent(parent[i])
|
|
|
|
return parent[i]
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-07-17 04:07:25 +00:00
|
|
|
minimum_spanning_tree_cost = 0
|
|
|
|
minimum_spanning_tree = []
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-07-17 04:07:25 +00:00
|
|
|
for edge in edges:
|
|
|
|
parent_a = find_parent(edge[1])
|
|
|
|
parent_b = find_parent(edge[2])
|
|
|
|
if parent_a != parent_b:
|
|
|
|
minimum_spanning_tree_cost += edge[3]
|
|
|
|
minimum_spanning_tree.append(edge)
|
|
|
|
parent[parent_a] = parent_b
|
|
|
|
|
|
|
|
print(minimum_spanning_tree_cost)
|
|
|
|
for edge in minimum_spanning_tree:
|
|
|
|
print(edge)
|