PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#DSU0000007. Thi công đường phố (Road Construction)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Thi công đường phố (Road Construction)
Road Construction
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
A city has intersections and bidirectional roads. Find the minimum number of roads to add so that every intersection is reachable from every other intersection.
Input
The first line contains two integers . Each of the next lines contains two integers , describing a road between intersections and . Intersections are numbered from to .
Output
Print the minimum number of additional roads required.
Subtasks
- Subtask 1 (20 points): ; ; ; ; no repeated edges.
- Subtask 2 (30 points): ; ; ; ; no repeated edges.
- Subtask 3 (50 points): ; ; ; ; no repeated edges.
Examples
Input
4 2
1 2
3 4
Output
1
Explanation
The intersections form two groups: and . One new road can connect them.