Team Up

4.8

4 votes
Problem

N children want to play a game, so they need to form teams. Each child has strength. The strength of the ith 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:

1T10

2N2104

1Q105

Query type 1 or type 3 (1 A B or 3 A B):

1AN

1BN

A!=B

The query of type 2(2 A):

1AN

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

For test case 1: 
We have 4 children, and we perform queries in the following manner:
First query: 1 1 2
Combine team of 1 and 2 so team configuration will be {1, 2}, {3}, {4}.
Second query: 3 2 3
Move 2 from it’s team to team of 3 so configuration will be {1}, {2,3}, {4}.
Third query: 2 3
A team of 3 contains two elements {2, 3} so the size will be 2, and the strength will be 5.
Hence, the answer will be [2, 5].

For test case 2:
We have 3 children here and 2 queries as follows:
Initial team configuration will be {1}, {2}, {3}.
First query: 2 1
Its output will be 1 1 as size is 1 and strength is also 1.
Second query: 2 2
Its output will be 1 2 as size is 1 and strength is 2.

Editor Image

?