#SRD0000120. Truy vấn liên thông trực tuyến bắt buộc (Forced Online Queries Problem)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Truy vấn liên thông trực tuyến bắt buộc (Forced Online Queries Problem)
Forced Online Queries Problem
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected graph with vertices numbered from to . Initially, the graph has no edges. Process operations in their given order.
Let be the answer to the most recent type 2 connectivity query; initially . Each operation contains with . Before applying the operation, decode the endpoints as
1 x y— Toggle an undirected edge: insert if it is absent, or remove it if it is present.2 x y— Connectivity query: its answer is if there is a path from to using currently present edges, and otherwise. Then set to that answer.
The graph may be disconnected and may contain cycles. The same edge can be toggled repeatedly. Every operation must use the current value of .
Input
- The first line contains .
- The following lines each contain , with , , .
- At least one operation is of type
2.
Output
Print one string consisting of 0 and 1 characters with no spaces. Its -th character is the answer to the -th type 2 query, in chronological order. End the output with a newline.
Subtasks
General guarantees: ; , ; at least one type 2 query.
- Subtask 1 (10 points): , ; all general guarantees remain in force.
- Subtask 2 (20 points): , ; all general guarantees remain in force.
- Subtask 3 (30 points): , ; all general guarantees remain in force.
- Subtask 4 (40 points): , ; all general guarantees remain in force (full original constraints).
Examples
Example 1
Input
5 9
1 1 2
1 1 3
2 3 2
1 2 4
2 3 4
1 2 4
2 3 4
1 1 3
2 4 3
Output
1010
Explanation
Initially . Edges and are inserted. The first query connects and through , so the answer is 1 and becomes . Subsequent operations shift both endpoints by one modulo . The four query answers are 1, 0, 1, 0, printed together as 1010.
Example 2
Input
3 9
1 1 2
1 2 3
1 3 1
2 1 3
1 3 2
2 2 3
1 1 2
2 1 2
2 1 2
Output
1101
Explanation
In the second example, the initial triangle connects all three vertices. The decoded toggle operations then lead to query answers 1, 1, 0, 1; concatenating them gives 1101.