Dsa
Union Find
Medium
Department Collaboration
In a company with multiple departments, employees from different departments can collaborate on projects. The collaboration can form groups, and you are to find the number of distinct collaboration groups formed based on the pairs of employees that can work together.
Input Format:
The first line contains an integer n, the number of employees (1 ≤ n ≤ 1000).
The next line contains an integer m, the number of collaboration pairs (0 ≤ m ≤ 2000).
The following m lines each contain two integers a and b, indicating that employee a can collaborate with employee b.
Output Format:
Print the number of distinct collaboration groups.
Example:
Input:
4
3
1 2
2 3
4 1
Output:
2
Key concepts
union_finddepartmentscollaboration
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