Dsa
Graphs Dfs
Easy
Cave Exploration
You are tasked with exploring a cave system that can be represented as a graph. Each chamber in the cave is connected to other chambers by corridors. Given the number of chambers and corridors (edges), determine the number of chambers that can be reached from a starting chamber. Input the number of chambers 'n', followed by 'm', the number of corridors, then 'm' pairs of integers indicating which chambers are connected. Output the number of reachable chambers from the starting chamber 1.
Example:
Input:
5
4
1 2
1 3
2 4
3 5
Output:
4
Key concepts
graphsdfsexploration
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