#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 nn planets numbered from 00 to n1n - 1, 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 (u,v)(u, v) to denote a teleporter that starts at planet uu and ends at planet vv.

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 0,1,,k10, 1, \dots, k - 1 (knk \le n) are called special planets. Each time the tourist enters a special planet, they obtain one stamp.

Currently, for each planet ii (0ik20 \le i \le k - 2), a teleporter (i,i+1)(i, i + 1) has been built. These k1k - 1 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 w[0],w[1],,w[t]w[0], w[1],\dots , w[t] satisfying:

  • 1t1 \le t.
  • 0w[0]k10 \le w[0] \le k - 1.
  • w[t]=w[0]w[t] = w[0].
  • For each ii (0it10 \le i \le t - 1), there exists a teleporter (w[i],w[i+1])(w[i], w[i + 1]).

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)
  • nn: the number of planets.
  • kk: the number of special planets.
  • This function is called exactly once, before any call to add_teleporter.
int add_teleporter(int u, int v)
  • uu and vv: the start and destination planets of the teleporter being added.
  • This function is called at most mm times (see the “Constraints” section for the range of mm).
  • If after adding teleporter (u,v)(u, v) it is possible for the tourist to obtain infinitely many stamps, the function must return 11. Otherwise, it should return 00.
  • Once the function returns 11, your program will be terminated.

Input Format

The sample grader reads the input in the following format:

  • Line 11: n m kn\ m\ k.
  • Line 2+i2 + i (0im10 \le i \le m - 1): u[i] v[i]u[i]\ v[i].

The sample grader first calls init, and then calls add_teleporter in the order i=0,1,,m1i = 0, 1,\dots , m - 1 with u=u[i]u = u[i] and v=v[i]v = v[i].

Output Format

The program needs to print the index (from 00 to m1m - 1, inclusive) of the first call to add_teleporter that returns 11. If all calls to add_teleporter return 00, your program should output mm.

If some call to add_teleporter returns an integer other than 00 or 11, the sample grader will print 1-1, 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

  • 1n3×1051\le n\le 3\times 10^5
  • 1m5×1051\le m\le 5\times 10^5
  • 1kn1\le k\le n

For each call to the add_teleporter function:

  • 0un10\le u\le n-1 and 0vn10\le v\le n-1
  • Before teleporter (u,v)(u,v) is added, there is no teleporter from planet uu to planet vv.

Subtasks

  1. (22 points) n=kn=k, n100n\le 100, m300m\le 300
  2. (1010 points) n100n\le 100, m300m\le 300.
  3. (1818 points) n103n\le 10^3, m5×103m\le 5\times 10^3
  4. (3030 points) n3×104n\le 3\times 10^4, m5×104m\le 5\times 10^4, k103k\le 10^3
  5. (4040 points) No additional constraints.

Examples

Example 11

Consider the following function call:

init(6, 3)

In this example, there are 66 planets and 33 special planets. Planets numbered 0,1,20, 1, 2 are special planets. The initial teleporters are (0,1)(0, 1) and (1,2)(1, 2).

Suppose the grader performs the following calls:

  • (0) add_teleporter(3, 4): should return 00.
  • (1) add_teleporter(5, 0): should return 00.
  • (2) add_teleporter(4, 5): should return 00.
  • (3) add_teleporter(5, 3): should return 00.
  • (4) add_teleporter(1, 4): in this case, it is possible to obtain infinitely many stamps. For example, the tourist can start from planet 00 and travel in the order 1,4,5,0,1,4,5,0,1, 4, 5, 0, 1, 4, 5, 0,\dots. Therefore, the function should return 11, 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 00 to 44 in the order they are added.

Example 22

Consider the following function call:

init(4, 2)

In this example, there are 44 planets and 22 special planets. Planets numbered 00 and 11 are special planets. The initial teleporter is (0,1)(0, 1).

Suppose the grader performs the following call:

  • add_teleporter(1, 1): after adding teleporter (1,1)(1, 1), we can obtain infinitely many stamps. For example, a tourist can start from planet 11 and use teleporter (1,1)(1, 1) to reach planet 11 infinitely many times. Therefore, the function should return 11, and then your program will be terminated.

The attachment package also includes another sample input and output.

Translated by ChatGPT 5