Dsa
Union Find
Medium
The Influencer Network
A social network has N users. Some users are 'Influencers'. A user is 'Reached' if they are an Influencer or if they are in the same social circle (connected component) as at least one Influencer. Given the initial Influencers and a sequence of friendships, find out how many 'Commoners' (non-influencers) are Reached after all friendships are formed.
Input Format:
First line: N (users), M (friendships), I (number of influencers).
Second line: I space-separated integers (IDs of influencers).
Next M lines: Two integers u and v (a friendship between u and v).
Output Format:
One integer representing the count of non-influencer users who are Reached.
Example:
Input:
5 2 1
0
1 2
2 3
Output:
0
Explanation:
Influencer: {0}. Friendships: (1,2), (2,3). Circles: {0}, {1,2,3}, {4}. Only influencer 0 is reached, but 0 is not a commoner. Total commoners reached: 0.
Key concepts
union_findboolean_propagation
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