Dsa
Union Find
Easy
Social Network Groups
In a social network, users can follow each other. You are given a series of follow relationships between users. Your task is to find the number of unique groups of users who are directly or indirectly connected through following. Each user is represented by a unique integer ID.
Input format:
- The first line contains an integer M (1 ≤ M ≤ 10^5), the number of follow relationships.
- The next M lines each contain two integers A and B, representing that user A follows user B.
Output format:
- Print a single integer representing the number of unique groups of connected users.
Example:
Input:
4
1 2
2 3
3 1
4 5
Output:
2
Key concepts
union_findgrouping
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