#GL0000331. Kết nối vòng tròn với chi phí nhỏ nhất (Ring MST)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Kết nối vòng tròn với chi phí nhỏ nhất (Ring MST)
Ring MST
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Initially an undirected graph has vertices numbered through and no edges. There are types of operations.
For operation type , choose any vertex with and add an undirected edge between and . This single edge costs . You may use each operation type any number of times, including zero.
Find the minimum total cost required to make the graph connected, or output if it is impossible.
One operation adds exactly one edge; it does not add every edge of that type simultaneously.
Input
The first line contains . Each of the following lines contains .
Output
Print the minimum total cost, or -1 if impossible.
Subtasks
- Subtask 1 (20 points): , , , .
- Subtask 2 (30 points): , , , .
- Subtask 3 (50 points): , , , .
Examples
Example 1
Input
4 2
2 3
3 5
Output
11
Explanation
Two edges of step cost each, connecting to and to . One edge of step connects the two components for , giving .
Example 2
Input
6 1
3 4
Output
-1
Explanation
Step on six vertices connects only opposite pairs; the graph cannot become connected.
Note: Each input file contains one case using the specified format.