#abc350g. G - Mediator

G - Mediator

Score: 600600 points

问题陈述

注意特殊的输入格式和比平时更小的内存限制。

有一个无向图,顶点为 1,2,,N1, 2, \dots, N,最初没有边。
你需要处理这个图上的 QQ 个查询:

1 $u$ $v$

类型 11:在顶点 uuvv 之间添加一条边。
在添加边之前,uuvv 属于不同的连通分量(即,图始终保持为森林)。

2 $u$ $v$

类型 22:如果存在一个顶点与 uuvv 都相邻,则报告其顶点编号;否则,报告 00
鉴于图始终保持为森林,这个查询的答案唯一确定。

查询以加密形式给出。
原始查询由三个整数 A,B,CA, B, C 定义,加密查询由三个整数 a,b,ca, b, c 给出。
XkX_k 是第 kk 个类型-22 查询的答案。定义 Xk=0X_k = 0 对于 k=0k = 0
根据给定的 a,b,ca, b, c 恢复 A,B,CA, B, C 如下:

  • ll 是此查询之前给出的类型-22 查询的数量(不包括查询本身)。然后,使用以下方法:
    • $A = 1 + (((a \times (1+X_l)) \mod 998244353) \mod 2)$
    • $B = 1 + (((b \times (1+X_l)) \mod 998244353) \mod N)$
    • $C = 1 + (((c \times (1+X_l)) \mod 998244353) \mod N)$

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

Beware the special input format and the smaller memory limit than usual.

There is an undirected graph with vertices 1,2,,N1, 2, \dots, N, initially without edges.
You need to process the following QQ queries on this graph:

1 $u$ $v$

Type 11: Add an edge between vertices uu and vv.
Before adding the edge, uu and vv belong to different connected components (i.e., the graph always remains a forest).

2 $u$ $v$

Type 22: If there is a vertex adjacent to both uu and vv, report its vertex number; otherwise, report 00.
Given that the graph always remains a forest, the answer to this query is uniquely determined.

The queries are given in an encrypted form.
The original query is defined by three integers A,B,CA, B, C, and the encrypted query is given as three integers a,b,ca, b, c.
Let XkX_k be the answer to the kk-th type-22 query. Define Xk=0X_k = 0 for k=0k = 0.
Restore A,B,CA, B, C from the given a,b,ca, b, c as follows:

  • Let ll be the number of type-22 queries given before this query (not counting the query itself). Then, use the following:
    • $A = 1 + (((a \times (1+X_l)) \mod 998244353) \mod 2)$
    • $B = 1 + (((b \times (1+X_l)) \mod 998244353) \mod N)$
    • $C = 1 + (((c \times (1+X_l)) \mod 998244353) \mod N)$

Constraints

  • All input values are integers.
  • 2N1052 \le N \le 10^5
  • 1Q1051 \le Q \le 10^5
  • 1u<vN1 \le u < v \le N
  • 0a,b,c<9982443530 \le a,b,c < 998244353

Input

The input is given from Standard Input in the following format:

NN QQ

Query\rm{Query}1_1

Query\rm{Query}2_2

\vdots

Query\rm{Query}Q_Q

Output

Let kk be the number of type-22 queries. Print kk lines.
The ii-th line should contain the answer to the ii-th type-22 query.

Sample Input 1

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

Sample Output 1

0
2
0
2
6
0
1

After decrypting all queries, the input is as follows:

6 12
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

This input has a 66-vertex graph and 1212 queries.

  • The first query is 2 1 3.
    • No vertex is adjacent to both vertex 11 and vertex 33, so report 00.
  • The second query is 1 2 6.
    • Add an edge between vertices 22 and 66.
  • The third query is 1 2 4.
    • Add an edge between vertices 22 and 44.
  • The fourth query is 1 1 3.
    • Add an edge between vertices 11 and 33.
  • The fifth query is 2 4 6.
    • The vertex adjacent to both vertices 44 and 66 is vertex 22.
  • The sixth query is 2 1 4.
    • No vertex is adjacent to both vertices 11 and 44, so report 00.
  • The seventh query is 1 5 6.
    • Add an edge between vertices 55 and 66.
  • The eighth query is 1 1 2.
    • Add an edge between vertices 11 and 22.
  • The ninth query is 2 1 4.
    • The vertex adjacent to both vertices 11 and 44 is vertex 22.
  • The tenth query is 2 2 5.
    • The vertex adjacent to both vertices 22 and 55 is vertex 66.
  • The eleventh query is 2 3 4.
    • No vertex is adjacent to both vertices 33 and 44, so report 00.
  • The twelfth query is 2 2 3.
    • The vertex adjacent to both vertices 22 and 33 is vertex 11.

Sample Input 2

2 1
377373366 41280240 33617925

Sample Output 2

The output may be empty.