Time limit
5s
Memory limit
512 MB
There is a forest with N vertices. The vertices are numbered from 1 to N, and initially there are no edges. Each vertex v has an integer Xv, and every initial value is 1.
Write a program that processes the following queries.
1 a b c: connect vertices a and b with an edge of weight c. This query is given only when the graph remains a forest after it is performed.2 a b: remove the edge connecting vertices a and b. This query is given only when that edge exists.3 a: first replace Xa with 1−Xa. Then, for the tree containing vertex a, let its vertices be v1,v2,…,vk and output the minimum value below.Here, dist(vi,vj) is the sum of the weights of all edges on the path from vi to vj.
The first line contains the number of vertices N and the number of queries Q. Each of the next Q lines contains one query.
The vertex numbers appearing in queries are encrypted and must be decoded before the query is executed. If the input vertex number is x and the sum of all outputs from type 3 queries processed so far is S, then the actual vertex number is
(x−1+S)modN+1Before the first type 3 query, use S=0. The edge weight c in a type 1 query is not encrypted.
For each type 3 query, output the computed value on its own line, in the order the queries are given.