Xor thing

0

0 votes
Graph Theory, Hard
Problem

Given a graph with vertices divided into N groups, each containing 2M vertices that are numbered from 0 inside the group.
Initially there is no edge. There are Q queries. Each query will be of one of the following 3 types:
1 i jp: For every vertex numbered x from 0 to 2m1 add an edge between vertex x from group i and vertex xp from group j.
2ix: Find the size of component containing vertex x in group i.
3: Count the total number of connected components.

Input Format:
First line consists of three space separated integers denoting N, M and Q.
Q lines follow, each containing one of the given 3 types of queries.

Output Format:
For every query of type 2 and 3 print the required answer in a new line.

Constraints:
1N105
1M40
1Q4×105
0p,x2M1
1i,jN

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The first query connects following vertices:
Vertex 0 of group 1 to vertex 1 of group 2.
Vertex 1 of group 1 to vertex 0 of group 2.

So connected components is 4.
Size of component containing vertex 0 of group 2 is 2.

Editor Image

?