Dsa
Union Find
Medium
Island Connectivity Checker
You are tasked with checking connections between a set of islands after a series of bridge constructions. Each bridge connects two islands, and you also need to handle queries asking if two islands are connected after the constructions. Implement a program that can process multiple operations to build bridges and check connectivity.
Input format:
- The first line contains an integer n (1 ≤ n ≤ 10^5), the number of operations.
- Each of the next n lines contains either 'B' (build bridge) and two integer values defining the islands (1 ≤ x, y ≤ 10^6), or 'C' (check connection) followed by two integers.
Output format:
- For each 'C x y' operation, output 'YES' if the islands are connected, otherwise print 'NO'.
Example:
Input:
6
B 1 2
B 2 3
C 1 3
C 1 4
B 4 5
C 1 5
Output:
YES
NO
YES
Key concepts
union_findgeographical
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