Dsa
Graphs Dfs
Medium
City Connection
You are given a list of pairs of cities that represent direct connections between them. Write a program to determine if there is a path connecting two specific cities.
Input format:
- First line contains an integer k, the number of connections.
- Next k lines contain two integers each, representing a connection between two cities.
- The last line contains two integers, city_a and city_b, to check for a connection.
Output format:
- A single line "YES" or "NO" indicating whether a path exists.
Example:
Input:
4
1 2
2 3
3 4
4 5
1 5
Output:
YES
Key concepts
graphsdfspathfinding
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