Under construction

Still being built. Things may change

Robot Assembly

Interview

Time limit4sMemory limit1024 MB

Summary
Maintain a growing set of unions between robot parts and answer, for each query part, how many parts currently belong to its robot.
Level

Medium6 of 10

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

Problem

Seonggyu has to assemble robots. A box contains parts of several robots mixed together. No part is labeled with the robot it belongs to. Ho-jae is an electronics major, so when he looks at two parts he can tell whether they belong to the same robot. Seonggyu decides to sort the parts by following Ho-jae's instructions.

Parts are represented by integers from 1 to 10^6. The robot that part i belongs to is also written as robot(i). For example, if part 11 and part 22 are known to be parts of robot A, then robot(11) means robot A, and robot(22) also means robot A.

Different robots do not share any part. That is, if a part belongs to robot A, it cannot belong to robot B.

Ho-jae gives two kinds of instructions.

  • He names two different parts and tells us that they belong to the same robot.
  • For a part i, he asks how many parts of robot(i) have been identified so far.

Initially, no information about the parts is known.

Input

The first line contains N, the number of Ho-jae's instructions. (1 ≤ N ≤ 10^6)

Then N instructions follow, one per line.

When he tells us that two parts belong to the same robot, the instruction is given in the form \(I\)* a b*. Part a and part b belong to the same robot. (1 ≤ a, b ≤ 10^6, a ≠ b, and a, b are integers)

When he asks how many parts a robot has, the instruction is given in the form Q c. It asks how many parts of robot(c) have been identified so far. (1 ≤ c ≤ 10^6, c is an integer)

At least one instruction of the form Q c appears in the input.

Output

For each instruction beginning with Q, print the number of identified parts of the corresponding robot, one per line.

Examples1

  1. Example 1

    Input
    4
    I 1 2
    I 3 2
    Q 1
    Q 4
    
    Expected output
    3
    1