#P8375. [APIO2022] 游戏
[APIO2022] 游戏
Background
This problem only supports C++ submissions. When submitting, you do not need to include the game.h header file; you only need to paste the contents of game.h from the attachment at the beginning of your code.
Problem Description
The Pharaohs discovered planets numbered from to , and built a one-way teleportation system between them. In this system, each teleporter connects a start planet and a destination planet. When a tourist uses a teleporter from its start planet, they will arrive at the corresponding destination planet. Note that the start planet and the destination planet may be the same planet. We use to denote a teleporter that starts at planet and ends at planet .
To promote the widespread use of the teleportation system, the Pharaohs designed a game for tourists to play while traveling. A tourist may start from any planet. Planets numbered () are called special planets. Each time the tourist enters a special planet, they obtain one stamp.
Currently, for each planet (), a teleporter has been built. These teleporters are called initial teleporters.
Teleporters are continuously built over time. As teleporters are added, it may become possible for a tourist to obtain infinitely many stamps. More precisely, this happens when there exists a planet sequence satisfying:
- .
- .
- .
- For each (), there exists a teleporter .
Note that a tourist can use both the initial teleporters and any teleporter that has already been built.
Your task is to help the Pharaohs check, after each new teleporter is added, whether a tourist can obtain infinitely many stamps.
Implementation Details
You need to implement the following functions:
init(int n, int k)
- : the number of planets.
- : the number of special planets.
- This function is called exactly once, before any call to
add_teleporter.
int add_teleporter(int u, int v)
- and : the start and destination planets of the teleporter being added.
- This function is called at most times (see the “Constraints” section for the range of ).
- If after adding teleporter it is possible for the tourist to obtain infinitely many stamps, the function must return . Otherwise, it should return .
- Once the function returns , your program will be terminated.
Input Format
The sample grader reads the input in the following format:
- Line : .
- Line (): .
The sample grader first calls init, and then calls add_teleporter in the order with and .
Output Format
The program needs to print the index (from to , inclusive) of the first call to add_teleporter that returns . If all calls to add_teleporter return , your program should output .
If some call to add_teleporter returns an integer other than or , the sample grader will print , and your program will be terminated immediately.
6 5 3
3 4
5 0
4 5
5 3
1 4
4
4 1 2
1 1
0
4 3 2
1 3
2 0
3 2
2
Hint
Constraints
- ;
- ;
- 。
For each call to the add_teleporter function:
- and ;
- Before teleporter is added, there is no teleporter from planet to planet .
Subtasks
- ( points) , , ;
- ( points) , .
- ( points) , 。
- ( points) , , 。
- ( points) No additional constraints.
Examples
Example
Consider the following function call:
init(6, 3)
In this example, there are planets and special planets. Planets numbered are special planets. The initial teleporters are and .
Suppose the grader performs the following calls:
- (0)
add_teleporter(3, 4): should return . - (1)
add_teleporter(5, 0): should return . - (2)
add_teleporter(4, 5): should return . - (3)
add_teleporter(5, 3): should return . - (4)
add_teleporter(1, 4): in this case, it is possible to obtain infinitely many stamps. For example, the tourist can start from planet and travel in the order . Therefore, the function should return , and then your program will be terminated.
The figure below illustrates this example. Special planets and initial teleporters are shown in bold. Teleporters added via add_teleporter are labeled from to in the order they are added.

Example
Consider the following function call:
init(4, 2)
In this example, there are planets and special planets. Planets numbered and are special planets. The initial teleporter is .
Suppose the grader performs the following call:
add_teleporter(1, 1): after adding teleporter , we can obtain infinitely many stamps. For example, a tourist can start from planet and use teleporter to reach planet infinitely many times. Therefore, the function should return , and then your program will be terminated.
The attachment package also includes another sample input and output.
Translated by ChatGPT 5