Team Up
Practice
4.8 (5 votes)
Data structures
Disjoint data structures
Basics of disjoint data structures
Problem
28% Success 1657 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code
\(N\) children want to play a game, so they need to form teams. Each child has strength. The strength of the \(i'th\) children is \(i\). Initially, no team is created, and all children are on their own, so there are \(N\) teams in the beginning.
You need to perform \(Q\) queries to help them in the team forming task. The query can be of 3 types as follow:
- \(1\) \(A\) \(B\): Combine the team where \(A\) belongs with the team where \(B\) belongs. If both belong to the same team, nothing needs to be done.
- \(2\) \(A\): Print the size and the total strength of the team where \(A\) belongs.
- \(3\) \(A\) \(B\): Move child \(A\) to the team where child \(B\) belongs. If both belong to the same team, nothing needs to be done.
For each query of type 2, you need to output two things, the team's size and total strength.
Input Format:
- The first line contains a single integer \(T\) denoting the number of test cases, then the test case follows.
- The first line of each test case contains two single space-separated integers, \(N\) and \(Q\).
- The following \(Q\) line contains the query one of any three types.
- It is guaranteed that there will be at least one query of type 2 in each test case.
Output Format:
For each test case, output an array that contains the answer for every query of type 2.
Constraints:
\(1 ≤ T ≤ 10\)
\(2 ≤ N ≤ 2*10^4\)
\(1 ≤ Q ≤ 10^5\)
Query type 1 or type 3 (1 A B or 3 A B):
\(1 ≤ A ≤ N\)
\(1 ≤ B ≤ N\)
\(A != B\)
The query of type 2(2 A):
\(1 ≤ A ≤ N\)
Submissions
Please login to view your submissions
Similar Problems
Points:30
4 votes
Tags:
Data StructuresDisjoint setMediumQueue
Points:30
10 votes
Tags:
ApprovedData StructuresDisjoint setMedium
Points:30
8 votes
Tags:
AlgorithmsApprovedData StructuresDepth First SearchDisjoint setMediumOpen
Editorial