Dsa
Graphs Bfs
Easy
Social Media Connection
In a social media application, users can follow one another. You are given the following data: the number of users and a list of pairs representing follow relationships. Write a function to find the number of users that can be reached from a given user through the follow relationships. Input:
The first line contains an integer N (the number of users).
The second line contains an integer M (the number of follow relationships).
The next M lines each contain two integers A and B, meaning user A follows user B.
The last line contains the user ID to check the reachability from.
Output:
A single integer representing the number of users that can be reached, excluding the user itself.
Example:
Input:
5
4
1 2
1 3
2 4
3 5
1
Output:
4
Key concepts
graphsbfssocial network
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