#P5607. Maintaining Sets and Union Queries
Maintaining Sets and Union Queries
Maintaining Sets and Union Queries
You are given \( m \) sets, numbered from \( 1 \) to \( m \), and initially all sets are empty. There will be exactly \( m \) operations. Each operation is one of the following two types:
- Type 1: Given two integers \( x \) and \( y \), insert \( y \) into set \( x \). It is guaranteed that \( y \) is not already present in set \( x \).
- Type 2: Given two integers \( x_1 \) and \( x_2 \), output the number of distinct elements in the union of set \( x_1 \) and set \( x_2 \).
Make sure your program processes all the operations in order.
inputFormat
The first line contains a single integer \( m \) which denotes both the number of sets and the number of operations.
Each of the following \( m \) lines represents an operation. An operation is in one of the following formats:
- For a Type 1 operation:
1 x y
- For a Type 2 operation:
2 x1 x2
Here, \( x, x1, x2 \) are indices of the sets (with \( 1 \le x, x1, x2 \le m \)) and \( y \) is an integer value.
outputFormat
For each operation of Type 2, output a single integer on a new line representing the number of distinct elements in the union of the two specified sets.
sample
5
1 1 10
1 2 20
2 1 2
1 1 30
2 1 1
2
2
</p>