Intermediate
Open
Pro
Merging Communities
You are managing n people, labeled 0 to n - 1, each starting in
their own separate community. You will process a sequence of
operations, each of one of two types:
["merge", a, b]: merge the communities containing personaand personbinto a single community (if they are already in the same community, do nothing).["size", a]: report the number of people currently in the same community as persona.
Return a list of the results of every "size" query, in order.
Example 1
Input: n = 5,
operations = [["merge",0,1],["merge",1,2],["size",0],["merge",3,4],["size",4]]
Output: [3, 2] — after merging 0-1 and 1-2, the community {0,1,2}
has size 3; after merging 3-4, that community has size 2.
Example 2
Input: n = 3, operations = [["size",0],["merge",0,1],["size",1]]
Output: [1, 2]
Constraints
1 <= n <= 10^51 <= operations.length <= 10^50 <= a, b < n
Share this question