Dsa
Union Find
Medium
Social Media Groups
In a social media application, users can form groups. Each group is identified by a unique user ID. Users can invite others to their groups, and some users may be in multiple groups. Your task is to find out the number of distinct groups formed after all invitations are processed.
Input Format:
- The first line contains an integer m (1 <= m <= 10^4), the number of invitation requests.
- The following m lines each contains two integers x and y (1 <= x, y <= 10^4), representing that user x invites user y to their group.
Output Format:
- Print the number of distinct groups formed.
Example:
Input:
4
1 2
2 3
4 5
4 6
Output:
2
Key concepts
union_finddisjoint_sets
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