#P8519. [IOI 2021] 钥匙

[IOI 2021] 钥匙

Background

Due to technical limitations, please do not use C++14 (GCC 9) to submit this problem.

This is an interactive problem. You only need to implement the function required in the code.

Your code does not need to include any extra header files, and you do not need to implement the main function.

Problem Description

Architect Timothy designed a new escape-room game.

In this game, there are nn rooms, numbered from 00 to n1n - 1. At the beginning, each room contains exactly one key. Each key has a type, and the type is an integer in the range 00 to n1n - 1. The key in room ii has type r[i]r[i]. Note that multiple rooms may contain keys of the same type, so the values r[i]r[i] are not necessarily pairwise distinct.

The game also has mm undirected corridors, numbered from 00 to m1m - 1. Corridor jj connects two different rooms u[j]u[j] and v[j]v[j]. There may be multiple corridors between the same pair of rooms.

Players need to collect keys and move between rooms through corridors. When a player uses corridor jj to move from room u[j]u[j] to room v[j]v[j], or from room v[j]v[j] to room u[j]u[j], we say the player passes through corridor jj. A player can pass through corridor jj only if the player has collected a key of type c[j]c[j].

At any time, when the player is in some room xx, the player can perform the following two operations:

  • Collect the key in room xx, whose type is r[x]r[x] (unless a key of that type has already been collected).
  • Pass through corridor jj, where u[j]=xu[j] = x or v[j]=xv[j] = x, and the player has already obtained a key of type c[j]c[j].

Note that any key the player has collected can be used forever and is never discarded.

Initially, the player starts the game in some room ss, with no keys. If the player starts from room ss and can reach room tt by performing a sequence of the two operations described above, then room tt is said to be reachable from room ss.

For each room i (0in1)i ~ ( 0 \le i \le n − 1), let p[i]p[i] be the number of rooms that can be reached starting from room ii. Timothy wants to know the set of indices ii for which p[i]p[i] is minimal.

Input Format

You need to implement the following function:

std::vector<int> find_reachable(
	std::vector<int> r, std::vector<int> u, 
	std::vector<int> v, std::vector<int> c
)
  • rr: a sequence of length nn. For each i (0in1)i ~ ( 0 \leq i \leq n − 1), the key type in room ii is r[i]r[i].
  • u,vu, v: two sequences of length mm. For each j (0jm1)j ~ ( 0 \leq j \leq m − 1), corridor jj connects rooms u[j]u[j] and v[j]v[j].
  • cc: a sequence of length mm. For each j (0jm1)j ~ ( 0 \leq j \leq m − 1), passing through corridor jj requires a key of type c[j]c[j].

Output Format

  • The function should return a sequence aa of length nn. For ii in 0in10 \leq i \leq n − 1, if p[i]p[j] (0jn1)p[i] \leq p[j] ~ (0 \leq j \leq n − 1), then a[i]=1a[i] = 1; otherwise, a[i]=0a[i] = 0.
4 5
0 1 1 2
0 1 0
0 2 0
1 2 1
1 3 0
3 1 2

0 1 1 0

7 10
0 1 1 2 2 1 2
0 1 0
0 2 0
1 2 1
1 3 0
2 3 0
3 4 1
3 5 2
4 5 0
4 6 2
5 6 1

0 1 1 0 1 0 1

3 1
0 0 0
0 1 0

0 0 1

Hint

Sample Explanation

For Example 11, consider the following call:

find_reachable([0, 1, 1, 2],
[0, 0, 1, 1, 3], [1, 2, 2, 3, 1], [0, 0, 1, 0, 2])

If the player starts the game from room 00, they can perform the following sequence of operations:

Current Room Operation
00 Collect key of type 00
Pass through corridor 00 to room 11
11 Collect key of type 11
Pass through corridor 22 to room 22
22 Pass through corridor 22 to room 11
11 Pass through corridor 33 to room 33

Therefore, room 33 is reachable starting from room 00. Similarly, we can construct operation sequences showing that all 44 rooms are reachable from room 00, so p[0]=4p[0] = 4. The table below shows the set of reachable rooms starting from each room:

Start Room ii Reachable Rooms p[i]p[i]
00 [0,1,2,3][0, 1, 2, 3] 44
11 [1,2][1, 2] 22
22
33 [1,2,3][1, 2, 3] 33

The minimum value of p[i]p[i] among all rooms is 22, which is achieved at i=1i = 1 or i=2i = 2. Therefore, the return value of this function call is [0,1,1,0][0, 1, 1, 0].

For Example 22: the minimum value of p[i]p[i] among all rooms is 22, which is achieved at i{1,2,4,6}i \in \{1, 2, 4, 6\}. Therefore, the return value of this function call is [0,1,1,0,1,0,1][0, 1, 1, 0, 1, 0, 1].

For Example 33: the minimum value of p[i]p[i] among all rooms is 11, which is achieved at i=2i = 2. Therefore, the return value of this function call is [0,0,1][0, 0, 1].

Constraints

  • 2n3×1052 \leq n \leq 3 \times 10 ^ 5.
  • 1m3×1051 \leq m \leq 3 \times 10 ^ 5.
  • 0r[i]n10 \leq r[i] \leq n - 1 (for all 0in10 \leq i \leq n - 1).
  • 0u[j],v[j]n10 \leq u[j], v[j] \leq n - 1 and u[j]v[j]u[j] \neq v[j] (for all 0jm10 \leq j \leq m - 1).
  • 0c[j]n10 \leq c[j] \leq n - 1 (for all 0jm10 \leq j \leq m - 1).

Subtasks

  1. (99 points) c[j]=0c[j] = 0 (for all 0jm10 \leq j \leq m − 1) and n,m200n, m \leq 200.
  2. (1111 points) n,m20n, m \leq 20.
  3. (1717 points) n,m2000n, m \leq 2000.
  4. (3030 points) c[j]29c[j] \leq 29 (for all 0jm10 \leq j \leq m − 1) and r[i]29r[i] \leq 29 (for all 0in10 \leq i \leq n − 1).
  5. (3333 points) No additional constraints.

Sample Grader

The sample grader reads the input in the following format:

  • Line 11: n mn ~ m.
  • Line 22: r[0] r[1]  r[n1]r[0] ~ r[1] ~ \cdots ~ r[n − 1].
  • Line 3+j3 + j (0jm1)( 0 \leq j \leq m − 1): u[j] v[j] c[j]u[j] ~ v[j] ~ c[j].

The sample grader prints the return value of the find_reachable function in the following format:

  • Line 11: s[0] s[1] s[n1]s[0] ~ s[1] \cdots ~ s[n − 1].

Translated by ChatGPT 5