Dsa
Graphs Dfs
Hard
Connected Cities
You are given a list of directed edges representing connections between cities. A city is accessible if there is a path from it to another city. Determine the number of distinct accessible city components.
Input:
- First line contains an integer n (1 <= n <= 1000), number of cities.
- Next lines describe the directed edges with two integers u v (1 <= u, v <= n), meaning there's an edge from city u to city v.
Output:
- A single integer representing the number of distinct accessible city components.
Example:
Input:
5
1 2
1 3
2 4
3 4
5 4
Output:
3
Key concepts
graphsdfsconnected-components
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