Dsa
Union Find
Medium
Library Book Groups
You are tasked with managing a library system where books can belong to multiple genres. Each genre is identified by a distinct integer. Books can be grouped into connected components, meaning if two genres are connected, any book from either genre can be classified under the combined genres. Given a list of connections between genres, you need to output the size of the largest connected component of genres in terms of the number of unique genres.
Input format:
- The first line contains an integer n (1 ≤ n ≤ 1000), the number of genres.
- The second line contains an integer m (0 ≤ m ≤ 10000), the number of connections.
- The following m lines contain two integers a and b (1 ≤ a, b ≤ n), representing a connection between genres a and b.
Output format:
- Print a single integer, the size of the largest connected component.
Example:
Input:
5
4
1 2
2 3
4 5
3 4
Output:
4
Key concepts
union_findgraphdisjoint_sets
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