Uni queries

3

2 votes
Graphs, Euler Tour and Path, Algorithms, Segment tree, Query, DSU, Depth First Search
Problem

You are given n (0n500000) and q (1q600000) where n is a number of vertexes and q is a number of queries. Initially, you have a graph with no edges and 0 is written on every vertex, then you need to process q queries.

You have three types of queries:

  • 1 type is to add an undirected edge between A and B. (1A,Bn)
    The graph is guaranteed to be simple (without self-loops and multiple edges) even after adding edges.
  • 2 type is to add number B to every number written on vertexes with are in the same component with A. (1An,1B1000000)
  • 3 type is to show the written number on A. (1An)

Input format

  • The first line contains an integer T (1T1000) representing the number of test cases that will follow.
  • The first line of each test case contains two integers n and q.
  • Next q lines in test case given queries in the following format:
    Type of query if the type of query is 3 there given one integer A. Otherwise there given two integers A and B.

Output format

For each query of 3 type, you should print answers on a different line.

Time Limit: 2
Memory Limit: 512
Source Limit:
Explanation

In first test case: 0 written on every vertex, so answer for vertex 1 is 0.

In second test case: 

1st query, you add 1 to vertex 1 only.

2nd query. you print the written number on vertex 1, which is 1.

3rd query,  you add edge between 1 and 2

4th query, you print the written number on vertex 1, which is 1.

5th query, you print the written number on vertex 2, which is 0.

Editor Image

?