Dsa
Union Find
Medium
Civil Engineering Project
In a civil engineering project, several construction teams are collaborating. Each team can form alliances with other teams. Given the number of teams and a list of alliances, determine if it's possible to build a single structure based on the provided agreements. An alliance is formed between two teams. If two teams are queried, return whether they can work together directly or through other alliances.
Example:
Input:
4
alliance 1 2
alliance 2 3
query 1 4
alliance 3 4
query 1 4
Output:
NO
YES
Key concepts
union_finddisjoint_setsconstruction
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