Dsa
Union Find
Hard
Gateway Alliances
In a network of N nodes, some nodes are designated as 'Gateways'. A connected component is considered 'Global' if it contains at least TWO different gateway nodes. Given the list of gateways and a sequence of M edges being added, after each edge addition, output the total number of nodes that currently belong to any 'Global' component.
Example:
Input:
5 2
0 4
3
0 1
1 2
2 4
Output:
0
0
5
(Gateways are 0 and 4. After (0,1) and (1,2), only one gateway (0) is in the component {0,1,2}. After (2,4), gateways 0 and 4 are connected, so all nodes {0,1,2,4} (and 3 is separate) are global? No, component {0,1,2,4} has 4 nodes).
Key concepts
union_findlogic_trackingconnectivity
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