#P6764. [APIO2020] 粉刷墙壁
[APIO2020] 粉刷墙壁
Description
It has been a while since the last time Pak Dengklek painted the walls in his house, so he wants to repaint them. The wall consists of sections, numbered from to . In this problem, we assume there are different colors, represented by integers from to (for example, red is represented by , blue by , and so on). Pak Dengklek wants to paint wall section with color .
To paint the wall, Pak Dengklek hired a contractor company with contractors, numbered from to . Unfortunately for Pak Dengklek, contractors are only willing to paint using colors they like. Specifically, contractor likes colors, and only wants to paint using one of the following colors: color , color , , or color .
Pak Dengklek can give the contractor company some instructions. In a single instruction, Pak Dengklek gives two parameters and , where and . The contractor company will assign contractor to paint wall section , where . If there exists an such that contractor does not like color , then that instruction is invalid.
Pak Dengklek has to pay for each instruction, so he wants to know the minimum number of instructions he must give so that every wall section is painted in its intended color, or determine that his goal is impossible. Each wall section may be painted multiple times, but every time it is painted, the color must be the one Pak Dengklek intends.
You must implement the function minimumInstructions:
minimumInstructions(N, M, K, C, A, B)- This function will be called exactly once by the grader.- : an integer representing the number of wall sections.
- : an integer representing the number of contractors.
- : an integer representing the number of colors.
- : an integer sequence of length , representing the intended color of each wall section.
- : an integer sequence of length , representing how many colors each contractor likes.
- : a sequence (of length ) whose elements are sequences, representing the specific colors each contractor likes.
- The function must return an integer: the minimum number of instructions Pak Dengklek needs to ensure the wall is painted as intended; return if it is impossible.
Input Format
The sample grader reads input in the following format:
N M K
C[0] C[1] ... C[N-1]
A[0] B[0][0] B[0][1] ... B[0][A[0]-1]
A[1] B[1][0] B[1][1] ... B[1][A[1]-1]
.
.
.
A[M-1] B[M-1][0] B[M-1][1] ... B[M-1][A[M-1]-1]
Output Format
The sample grader outputs the return value of the function minimumInstructions.
8 3 5
3 3 1 3 4 4 2 2
3 0 1 2
2 2 3
2 3 4
3
5 4 4
1 0 1 2 2
2 0 1
1 1
1 2
1 3
-1
Hint
In the first sample, , , , , , . Pak Dengklek can give the following instructions.
- , . This is a valid instruction. Contractor can paint wall section , contractor can paint wall section , and contractor can paint wall section .
- , . This is a valid instruction. Contractor can paint wall section , contractor can paint wall section , and contractor can paint wall section .
- , . This is a valid instruction. Contractor can paint wall section , contractor can paint wall section , and contractor can paint wall section .
It is easy to see that Pak Dengklek cannot achieve his goal with fewer than instructions, so minimumInstructions(8, 3, 5, [3, 3, 1, 3, 4, 4, 2, 2], [3, 2, 2], [[0, 1, 2], [2, 3], [3, 4]]) should return .
In the second sample, , , , , , . Since contractor only likes color , but no wall section is intended to be painted with that color, Pak Dengklek cannot give any valid instruction. Therefore, minimumInstructions(5, 4, 4, [1, 0, 1, 2, 2], [2, 1, 1, 1], [[0, 1], [1], [2], [3]]) should return .
For , let denote the number of contractors who like color .
Constraints
- .
- .
- .
- .
- .
- $0 \leq B[j][0] < B[j][1] < \dots < B[j][A[j] − 1] < K$.
- .
Subtask ( points)
- .
Subtask ( points)
- .
- .
- .
Subtask ( points)
- .
- .
Subtask ( points)
- .
- .
Subtask ( points)
- No additional constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号