Dsa
Union Find
Hard
Group Formation
You are tasked with organizing a team-building event for different departments in a company. The employees will form groups based on their assigned relations. Each employee has a unique ID and can relate to multiple other employees. Your goal is to determine how many distinct groups can be formed.
Input Format:
- The first line contains an integer n: the number of employees (1 ≤ n ≤ 1000).
- The following lines will contain pairs of integers (a, b) denoting that employee a is related to employee b (1 ≤ a, b ≤ n). Each pair is provided on a new line, and the input ends when a line with '0 0' is encountered.
Output Format:
- Print a single integer representing the number of distinct groups that can be formed.
Example:
Input:
5
1 2
2 3
4 5
0 0
Output:
2
Key concepts
union_findgraph
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