#SRD0000125. Đỉnh trung gian (Mediator)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đỉnh trung gian (Mediator)
Mediator
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected graph on vertices numbered to , initially without any edges. Process operations in order. Each decoded operation has one of these forms:
- Type
1 u v: Add an undirected edge between and . Before the addition, and belong to different connected components. Therefore, the graph is always a forest. - Type
2 u v: Find a vertex adjacent to both and in the current graph. Print its number, or0if 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 , each input line gives . Define
Let be the answer to the most recent type-2 query, or if no type-2 query has yet been processed. Decode the next operation as follows:
Then perform operation A B C. Update only when , using the answer just printed. If , leave unchanged.
All decoded operations are guaranteed valid, , 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 .
- Each of the next lines contains the encrypted integers .
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: , , , ; every decoded operation has , ; each edge addition joins different components, so the graph always remains a forest; changes only after a type-2 query.
- Subtask 1 (10 points): , ; all common guarantees apply.
- Subtask 2 (20 points): , ; all common guarantees apply.
- Subtask 3 (30 points): , ; all common guarantees apply.
- Subtask 4 (40 points): , ; 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, and have no common neighbor, so the first answer is
0. - After adding edges , , and , query
2 4 6returns2: vertices and are both adjacent to vertex . - The next
2 1 4query returns0. - After adding and , the remaining queries return
2,6,0, and1. In particular, is adjacent to both ; is adjacent to both ; and is adjacent to both .
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.