luogu#P5247. 【模板】动态图连通性

【模板】动态图连通性

Background

This is an unofficial, unmaintained mirror of LOJ #122. The original problem setter is EtaoinWu, and the original uploader on this site is unknown. The testdata of this mirror is not guaranteed to be the latest, so it is recommended to practice on LOJ.

Problem Description

This is a template problem.

You need to maintain an undirected simple graph (that is, an undirected graph with no self-loops and no multiple edges). You are required to add an edge, delete an edge, and query whether two vertices are connected.

0.0.: Add an edge. It is guaranteed that it does not exist.
1.1.: Delete an edge. It is guaranteed that it exists.
2.2.: Query whether two vertices are connected.

To keep the solution online, this problem uses a special input method.

Assume you maintain a variable last\text{last}, with initial value 00.

For each input vertex xx, the actual vertex index used in the query or modification is x xor lastx \text{ xor } \text{last}, where xor\text{xor} is the bitwise XOR operation.

After decoding each query u,vu,v, if they are connected, then last\text{last} will be updated to uu; otherwise it will be updated to vv.

Input Format

The first line contains two integers n,mn,m.

The next mm lines each contain three integers op,x,y\text{op},x,y. op\text{op} denotes the operation number.

Output Format

For each query with op=2op=2, output one line Y or N, indicating whether the two vertices are connected.

200 5
2 123 127
0 4 0
2 4 0
1 4 0
2 0 4
N
Y
N
4 10
0 1 2
0 2 3
0 3 1
2 1 4
0 0 7
2 5 0
1 3 2
2 0 5
1 0 2
2 0 5
N
Y
Y
N

Hint

Due to the addition of hack testdata, the data distribution is not as described below. The following is for reference only.

For test point 11, n200,m200n \leq 200,m \leq 200.

For test point 22, n=5,m30n=5,m \leq 30.

For test point 33, n=10,m1000n=10,m \leq 1000, and the number of queries is 900\geq 900.

For test point 44, n=300,m50000n=300,m \leq 50000.

For test point 55, n=5000,m200000n=5000,m \leq 200000, there is no operation 11, and about 70%70 \% are operation 22.

For test point 66, n=5000,m200000n=5000,m \leq 200000, there is no operation 11, and about 70%70 \% are operation 00.

For test point 77 and 88, n=100,m500000n=100,m \leq 500000.

For test point 99, n=5000,m500000n=5000,m \leq 500000, the graph is a tree, and its diameter is 30\leq 30.

For test point 1010, n=5000,m500000n=5000,m \leq 500000, the graph is a tree, and the degree of each vertex is 10\leq 10.

There is also some additional testdata with guarantees n5000,m500000n \leq 5000,m \leq 500000.

Translated by ChatGPT 5