Dsa
Union Find
Easy
City Reservoirs Connectivity
You have a number of reservoirs in a city connected by pipelines. The connectivity is described through a list of pairs of connected reservoirs. Given two reservoirs, determine if they can receive water from each other indirectly through existing connections. Start with an integer N (the number of reservoirs) and an integer M (the number of connections) followed by M pairs of reservoirs. Finally, two reservoirs will be specified. Your program should print 'YES' if the two reservoirs are connected, otherwise print 'NO'.
Example Input:
5 3
1 2
2 3
4 5
1 4
Output:
NO
Key concepts
union_findnetworkconnected_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