Dsa
Union Find
Easy
Library Book Management
In a library, every book can be either a standalone edition or part of a series. You're tasked to identify the distinct series of books given the relationships between them. Each relationship is denoted by pairs of books where a pair indicates that the two books belong to the same series. Write a program that determines the number of distinct book series given `n` pairs of relationships.
Input format:
- The first line contains an integer `n` (1 <= n <= 1000), the number of book relationships.
- The next `n` lines each contain two uppercase letters representing the books (e.g., 'A B').
Output format:
- A single integer representing the number of distinct book series.
Example:
Input:
5
A B
B C
D E
E F
G H
Output:
4
This indicates there are 4 distinct book series: {A, B, C}, {D, E, F}, {G}, and {H}.
Key concepts
union_finddata_structuresgraph
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