Dsa
Union Find
Easy
Team Merging
You are tasked with managing multiple teams in a company. Each team is represented by a unique integer. When two teams merge, they become a single team represented by the id of one of the original teams. Implement a union-find data structure to efficiently manage these team merges and output the size of the resulting team when all merges are processed.
Input format:
- The first line contains an integer N, representing the number of merges.
- The next N lines each contain two integers, A and B, indicating a merge between team A and team B.
Output format:
- Print a single integer indicating the total number of distinct teams left after all merges.
Example:
Input:
5
1 2
2 3
3 4
5 6
6 7
7 5
Output:
3
Key concepts
union_findteam_managementmerging
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