Dsa
Union Find
Warm-up
Organize Sports Teams
You are organizing a sports event and have a list of players. Each player can be a part of multiple teams. You also have a list of pairs of players that are best friends and can only play in the same team. Your task is to determine how many distinct teams can be formed under the given constraints.
Input Format:
- The first line contains an integer n (1 ≤ n ≤ 1000), the number of players.
- 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), indicating that player a and player b are best friends.
Output Format:
- Print the number of distinct teams that can be formed.
Example:
Input:
5
3
1 2
2 3
4 5
Output:
2
Input:
3
2
1 2
1 3
Output:
1
Key concepts
union_findteam_assignmentgrouping
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