[ABC350G] 在线森林共同邻居 (Mediator)
Score: points
Problem Statement
Beware the special input format and the smaller memory limit than usual.
There is an undirected graph with vertices , initially without edges.
You need to process the following queries on this graph:
1 $u$ $v$
Type : Add an edge between vertices and .
Before adding the edge, and belong to different connected components (i.e., the graph always remains a forest).
2 $u$ $v$
Type : If there is a vertex adjacent to both and , report its vertex number; otherwise, report .
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 , and the encrypted query is given as three integers .
Let be the answer to the -th type- query. Define for .
Restore from the given as follows:
-
Let be the number of type- 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.
-
-
-
-
Input
The input is given from Standard Input in the following format:
$N$ $Q$
$\rm{Query}$$_1$
$\rm{Query}$$_2$
$\vdots$
$\rm{Query}$$_Q$
Output
Let be the number of type- queries. Print lines.
The -th line should contain the answer to the -th type- 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 -vertex graph and queries.
-
The first query is
2 1 3. No vertex is adjacent to both vertex and vertex , so report . -
The second query is
1 2 6. Add an edge between vertices and . -
The third query is
1 2 4. Add an edge between vertices and . -
The fourth query is
1 1 3. Add an edge between vertices and . -
The fifth query is
2 4 6. The vertex adjacent to both vertices and is vertex . -
The sixth query is
2 1 4. No vertex is adjacent to both vertices and , so report . -
The seventh query is
1 5 6. Add an edge between vertices and . -
The eighth query is
1 1 2. Add an edge between vertices and . -
The ninth query is
2 1 4. The vertex adjacent to both vertices and is vertex . -
The tenth query is
2 2 5. The vertex adjacent to both vertices and is vertex . -
The eleventh query is
2 3 4. No vertex is adjacent to both vertices and , so report . -
The twelfth query is
2 2 3. The vertex adjacent to both vertices and is vertex .
Sample Input 2
2 1
377373366 41280240 33617925
Sample Output 2
The output may be empty.