Dsa
Union Find
Easy
Friends Circles
You are given a list of friendships represented as pairs of integers, where each integer corresponds to a person. Each pair indicates a direct friendship between two people. Write a program that calculates the number of unique friend circles, where a friend circle is defined as a group of persons who are directly or indirectly connected through friendships.
Input:
The first line contains an integer n (1 <= n <= 1000), the number of friendships. The next n lines contain two integers x and y (0 <= x, y <= 999), which indicate that person x is friends with person y.
Output:
Print the number of unique friend circles.
Example:
Input:
4
0 1
1 2
3 4
0 2
Output:
2
Key concepts
union_findsetsgraphs
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