Dsa
Union Find
Easy
Married Couples
In a city, couples form marriages that can be represented as pairs of spouses. Given the pairs, your task is to determine how many distinct marital unions exist.
Input Format:
The first line contains an integer m, the number of marital pairs. Each of the next m lines contains two integers a and b, representing a marriage between person a and person b.
Output Format:
Print a single integer denoting the number of distinct marital unions.
Example:
Input:
3
1 2
2 3
4 5
Output:
2
Key concepts
union_findrelationships
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