#GD0000225. Cây khung nhỏ nhất (Minimum Spanning Tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Cây khung nhỏ nhất (Minimum Spanning Tree)
Minimum Spanning Tree
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
Given an undirected weighted graph with vertices numbered from to and edges, find a minimum spanning tree: edges connecting every vertex without cycles and with minimum total weight.
If the graph is disconnected, no spanning tree exists. Any minimum spanning tree is accepted, but output its edges in lexicographic order after writing the smaller endpoint first.
Input
- The first line contains .
- Each of the next lines contains describing one undirected weighted edge. Vertices are zero-indexed.
Output
If the graph is disconnected, print Impossible.
Otherwise print the minimum total weight on the first line, followed by selected edges, one per line. Each edge has its smaller endpoint first, and the edge pairs must be lexicographically sorted.
Subtasks
- Subtask 1 (20 points): ; ; ; ; ; không có cạnh trùng.
- Subtask 2 (30 points): ; ; ; ; ; không có cạnh trùng.
- Subtask 3 (50 points): ; ; ; ; ; không có cạnh trùng.
Examples
Input
4 5
0 1 2
0 2 3
0 3 7
1 2 1
2 3 4
Output
7
0 1
1 2
2 3
Explanation
Select edges of weight , of weight , and of weight , totaling . They connect all four vertices without cycles. Print them sorted: .