Dsa
Union Find
Warm-up
Detect Circle of Friends
You are given a list of friend pairs, where each pair indicates that two people are friends and can be represented by unique integer IDs. Your task is to determine if there is a circle of friends, meaning if there exists a friend group where any two friends are indirectly connected through a series of friendships.
Input Format:
- The first line contains an integer n (1 ≤ n ≤ 1000), the number of unique people.
- The second line contains an integer m (0 ≤ m ≤ 10000), the number of friendship pairs.
- The next m lines contain two integers a and b (1 ≤ a, b ≤ n), representing a friendship between person a and person b.
Output Format:
- Print "Yes" if there exists at least one circle of friends, otherwise print "No".
Example:
Input:
4
3
1 2
2 3
3 4
Output:
Yes
Input:
5
2
1 2
3 4
Output:
No
Key concepts
union_findgraphconnected_components
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