Dsa
Union Find
Hard
Company Merge
Two companies want to merge, and you are tasked to help determine the total number of distinct branches after all possible merges. Each branch is identified by a unique ID. You receive pairs of IDs indicating which branches can merge together. Determine the number of distinct branches remaining after all merges have been processed.
Input Format:
The first line contains an integer n (branch count).
The second line contains an integer m (number of merge request pairs).
The next m lines each contain two integers u and v, indicating branches u and v can merge.
Output Format:
Print the total number of distinct branches after all merges.
Example:
Input:
5
4
1 2
2 3
1 4
4 5
Output:
1
Key concepts
union_findmergingbusiness
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