PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000125. Đỉnh trung gian (Mediator)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đỉnh trung gian (Mediator)

Mediator

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected graph on NN vertices numbered 11 to NN, initially without any edges. Process QQ operations in order. Each decoded operation has one of these forms:

  • Type 1 u v: Add an undirected edge between uu and vv. Before the addition, uu and vv belong to different connected components. Therefore, the graph is always a forest.
  • Type 2 u v: Find a vertex ww adjacent to both uu and vv in the current graph. Print its number, or 0 if none exists. Since a forest cannot contain a cycle, two distinct vertices cannot have two different common neighbors: the answer is unique.

Important — the input operations are encrypted. Instead of the original three integers A,B,CA,B,C, each input line gives a,b,ca,b,c. Define

P=998244353.P=998244353.

Let xx be the answer to the most recent type-2 query, or 00 if no type-2 query has yet been processed. Decode the next operation as follows:

$$\begin{aligned} A &= 1 + \bigl((a(1+x)\bmod P)\bmod 2\bigr),\ B &= 1 + \bigl((b(1+x)\bmod P)\bmod N\bigr),\ C &= 1 + \bigl((c(1+x)\bmod P)\bmod N\bigr). \end{aligned}$$

Then perform operation A B C. Update xx only when A=2A=2, using the answer just printed. If A=1A=1, leave xx unchanged.

All decoded operations are guaranteed valid, 1≤B<C≤N1\le B<C\le N, and every added edge joins two different connected components. You must not reorder the operations or use future answers when decrypting the current one.

Input

  • The first line contains integers N,QN,Q.
  • Each of the next QQ lines contains the encrypted integers a,b,ca,b,c.

Output

For each decoded type-2 query, print a single line containing the unique common neighbor, or 0 if none exists. If there are no type-2 queries, the output is empty.

Subtasks

Every subtask satisfies all of these common guarantees: 2≤N2\le N, 1≤Q1\le Q, 0≤a,b,c<P0\le a,b,c<P, P=998244353P=998244353; every decoded operation has A∈{1,2}A\in\{1,2\}, 1≤B<C≤N1\le B<C\le N; each edge addition joins different components, so the graph always remains a forest; xx changes only after a type-2 query.

  • Subtask 1 (10 points): 2≤N≤402\le N\le40, 1≤Q≤1001\le Q\le100; all common guarantees apply.
  • Subtask 2 (20 points): 2≤N≤1 5002\le N\le1\,500, 1≤Q≤1 5001\le Q\le1\,500; all common guarantees apply.
  • Subtask 3 (30 points): 2≤N≤20 0002\le N\le20\,000, 1≤Q≤20 0001\le Q\le20\,000; all common guarantees apply.
  • Subtask 4 (40 points): 2≤N≤100 0002\le N\le100\,000, 1≤Q≤100 0001\le Q\le100\,000; all common guarantees apply. These are the full original constraints.

Examples

Example 1

Input

6 12
143209915 123840720 97293110
89822758 207184717 689046893
67757230 270920641 26993265
952858464 221010240 871605818
730183361 147726243 445163345
963432357 295317852 195433903
120904472 106195318 615645575
817920568 27584394 770578609
38727673 250957656 506822697
139174867 566158852 412971999
205467538 606353836 855642999
159292205 319166257 51234344

Output

0
2
0
2
6
0
1

Explanation of Example 1

The decoded operations, in order, are:

2 1 3
1 2 6
1 2 4
1 1 3
2 4 6
2 1 4
1 5 6
1 1 2
2 1 4
2 2 5
2 3 4
2 2 3
  • Initially, 11 and 33 have no common neighbor, so the first answer is 0.
  • After adding edges (2,6)(2,6), (2,4)(2,4), and (1,3)(1,3), query 2 4 6 returns 2: vertices 44 and 66 are both adjacent to vertex 22.
  • The next 2 1 4 query returns 0.
  • After adding (5,6)(5,6) and (1,2)(1,2), the remaining queries return 2, 6, 0, and 1. In particular, 22 is adjacent to both 1,41,4; 66 is adjacent to both 2,52,5; and 11 is adjacent to both 2,32,3.

Example 2

Input

2 1
377373366 41280240 33617925

Output

Explanation of Example 2

The only decoded operation adds an edge. Since no query requests an answer, the program prints nothing.