PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#DSU0000004. Xây thêm đường (Building Roads)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Xây thêm đường (Building Roads)
Building Roads
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
A country has cities and bidirectional roads. Build the minimum number of additional roads so that every city can be reached from every other city.
Input
The first line contains two integers . Each of the next lines contains two integers , describing a bidirectional road between cities and . Cities are numbered from to .
Output
Print the minimum number of new roads on the first line. Then print lines, each containing the endpoints of a new road. Any valid optimal construction is accepted.
Subtasks
- Subtask 1 (20 points): ; ; ; ; no two existing roads connect the same pair of cities.
- Subtask 2 (30 points): ; ; ; ; no two existing roads connect the same pair of cities.
- Subtask 3 (50 points): ; ; ; ; no two existing roads connect the same pair of cities.
Examples
Input
4 2
1 2
3 4
Output
1
1 3
Explanation
The original components are {1,2} and {3,4}. One edge between them is sufficient.