Dsa
Union Find
Easy
Friendship Connections
In a school, students can connect through friendships. Your task is to determine how many distinct friendship circles exist in the school. Each circle is represented by students who are either directly or indirectly friends. Write a function that reads the number of friendship pairs followed by the pairs themselves, and outputs the number of distinct friendship circles.
Input format:
- The first line contains an integer F, the number of friendship pairs.
- The following F lines each contain two integers S1 and S2, representing a friendship between student S1 and student S2.
Output format:
- A single integer representing the number of distinct friendship circles.
Example:
Input:
3
1 2
2 3
4 5
Output:
2
Key concepts
union_findfriendshipnetwork_analysis
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