Under construction

Still being built. Things may change

Unite to Survive

Interview

Time limit2sMemory limit1024 MB

Summary
Given N sets, process commands that merge set b into set a and empty set b, and print the size of a queried set.
Level

Medium6 of 10

Topics
Hash map, Union-find, Implementation
Solved
No attempts yet

Problem

Given NN sets S1,S2,…,SNS_1, S_2, \ldots, S_N, process QQ commands as follows.

  • 1 a b: Replace SaS_a with Sa∪SbS_a \cup S_b, and replace SbS_b with the empty set. (1≤a,b≤N1 \le a, b \le N; a≠ba \ne b)
  • 2 a: Print the size of SaS_a. (1≤a≤N1 \le a \le N)

Input

The first line contains NN and QQ. (1≤N,Q≤500,0001 \le N, Q \le 500{,}000)

Each of the next NN lines describes SiS_i. The line starts with its size nin_i, followed by the elements sijs_{ij} of SiS_i. (1≤∑ni≤500,0001 \le \sum n_i \le 500{,}000; 1≤sij≤1091 \le s_{ij} \le 10^9; sij≠siks_{ij} \ne s_{ik} for all k≠jk \ne j)

Each of the next QQ lines contains one command described above. All input numbers are integers, and at least one command 2 a is given.

Output

For each command 2 a, print the result on its own line.

Examples1

  1. Example 1

    Input
    3 11
    2 5 1
    3 2 4 7
    4 8 5 2 6
    2 1
    2 2
    2 3
    1 1 3
    2 1
    2 3
    1 2 3
    2 2
    1 2 1
    2 1
    2 2
    
    Expected output
    2
    3
    4
    5
    0
    3
    0
    7