Dsa
Union Find
Medium
Redundant Grids
An electrical grid consists of N nodes. A power cluster is 'Redundant' if it contains at least one cycle (meaning there's more than one path between at least two nodes in that cluster). Given M cables added one by one, find out how many nodes are part of a Redundant cluster after each cable is added.
Input Format:
First line: N (nodes), M (cables).
Next M lines: Two integers u and v (a cable connecting node u and v).
Output Format:
M lines: The number of nodes belonging to clusters that are Redundant.
Example:
Input:
3 3
0 1
1 2
2 0
Output:
0
0
3
Explanation:
- (0,1): No cycle. Redundant nodes: 0.
- (1,2): No cycle. Redundant nodes: 0.
- (2,0): Cycle formed in cluster {0,1,2}. All 3 nodes are now in a redundant cluster.
Key concepts
union_findcycle_detection
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