#K15951. Connecting the Intersections
Connecting the Intersections
Connecting the Intersections
You are given a city with (N) intersections numbered from 1 to (N) and (M) bidirectional roads connecting some pairs of intersections. The network might be disconnected. Your task is to determine the minimum number of additional roads required to connect all intersections in such a way that there is exactly one simple path between any two intersections (i.e. the resulting graph forms a tree). In other words, if the graph has (C) connected components, you need to add (C-1) roads to connect them all.
inputFormat
The first line contains two integers (N) and (M) ((1 \leq N \leq 10^5), (0 \leq M \leq 10^5)), representing the number of intersections and the number of roads, respectively. Each of the following (M) lines contains two integers (u) and (v), indicating there is a road connecting intersection (u) and intersection (v).
outputFormat
Output a single integer, the minimum number of new roads needed so that all intersections become connected, forming a tree.## sample
6 3
1 2
2 3
4 5
2