Dsa
Union Find
Hard
Social Network Separation
In a social network, users can follow each other to form groups. You will be given a set of follow relationships between users. Your task is to determine how many separate groups of users exist. A group is defined as a set of users where each user can reach every other user in that group by following.
Input Format:
The first line contains two integers n (number of users) and m (number of relationships).
The next m lines each define a pair of users (u, v) indicating that user u follows user v.
Output Format:
Print the number of separate groups of users.
Example:
Input:
5 4
1 2
2 3
3 4
1 5
Output:
2
Key concepts
union_findgroupingcomponents
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