Dsa
Graphs Bfs
Easy
City Connection Problem
A transportation network consists of cities connected by roads. Each road connects two cities and you need to determine the number of disconnected sets of cities in the network. Each set is defined as a group of cities that are reachable from each other via the roads. Your job is to write a program that reads the number of cities and the connections and outputs the number of disconnected city sets.
Example:
Input:
4 2
0 1
1 2
Output:
2
Key concepts
graphsbfscity connections
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now