Dsa
Graphs Bfs
Easy
City Road Maintenance
A city has a number of roads connecting different neighborhoods. In certain cases, two neighborhoods might be linked indirectly through other neighborhoods. You want to find out if there is a route from a starting neighborhood to a target neighborhood. Write a program that takes a list of roads and checks for the existence of a path from the starting neighborhood to the target neighborhood.
Input Format:
- The first line contains an integer n (number of neighborhoods).
- The second line contains an integer m (number of roads).
- The next m lines contain two integers a and b representing a road between neighborhoods a and b.
- The last line contains two integers start and target, the neighborhoods to check.
Output Format:
- Print "YES" if a route exists, otherwise print "NO".
Example:
Input:
4
3
1 2
2 3
1 4
4 3
Output:
YES
Key concepts
bfsgraphpath-existence
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