Dsa
Union Find
Medium
Metropolitan Growth
You are a city planner overseeing N settlements (labeled 0 to N-1). Initially, each settlement is its own independent district. You are given a threshold K. A district is considered 'Metropolitan' if it contains K or more connected settlements. As roads are built between settlements, districts merge. Your task is to output the total number of people living in Metropolitan districts after each road is built, assuming each settlement has exactly 1 person.
Input Format:
The first line contains three integers: N (total settlements), K (threshold), and M (number of roads).
The next M lines each contain two integers u and v, representing a road being built between settlement u and settlement v.
Output Format:
Print M lines, where the i-th line is the total count of people in Metropolitan districts after the i-th road is built.
Example:
Input:
5 3 3
0 1
1 2
3 4
Output:
0
3
3
Explanation:
- After (0,1): District {0,1} has size 2 (<3). Total: 0.
- After (1,2): District {0,1,2} has size 3 (>=3). Total: 3.
- After (3,4): District {3,4} has size 2 (<3). Total: 3 (only the first district is Metropolitan).
Key concepts
union_findsize_tracking
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