#P8893. 「UOI-R1」智能推荐

「UOI-R1」智能推荐

Background

The testdata has been strengthened.

Problem Description

There are now NN problems.

The days are numbered starting from 00. Each day you may solve several problems. You can only solve problems that have been recommended before or are recommended on that day (each problem can be solved at most once). On day 00, the intelligent recommendation system will recommend pp problems.

The recommendation rules are as follows:

For problem ii, if it can possibly be recommended, there will be a problem set sis_i. If and only if you have solved every problem in sis_i, and among them there is at least one problem solved on the current day, then problem ii will be recommended on the next day.

You want to solve problem KK. What is the earliest day on which you can achieve this?

Input Format

The first line contains three integers N,K,pN, K, p, with meanings as described in the statement.

The second line contains pp integers, indicating the indices of the problems recommended on day 00.

The third line contains an integer RR, indicating that there are RR recommendation rules.

The next RR lines each contain one rule, in the following format:

An integer viv_i, the index of the problem to be recommended. Then an integer sis_i, the number of problems that must be solved in order for this problem to be recommended. Then sis_i integers pip_i, representing each problem that must be solved.

Output Format

Output one integer, the minimum day on which you can achieve the goal.

If problem KK can never be solved no matter what, output -1.

5 5 2
1 2
3
3 2 1 2
4 3 1 2 3
5 3 1 3 4
3
1 1 1
1
0
0
7 7 2
1 2
2
3 2 1 2
6 2 1 2
-1
见文件附件的 rec4.in
见文件附件的 rec4.ans

Hint

[Sample Explanation #1]

On day 00, problems 1,21, 2 were recommended, and both were solved.

On day 11, problem 33 was recommended and solved.

On day 22, problem 44 was recommended and solved.

On day 33, problem 55 was recommended, which is problem KK, and it was solved.

So problem KK can be solved by day 33.

[Sample Explanation #2]

On day 00, problem 11 was recommended, which is problem KK, and it was solved.
So it was completed on day 00.

[Constraints]

Let si\left| s_i \right| denote, in the ii-th recommendation rule, the total number of problems that must be solved if viv_i is to be recommended.

For 30%30\% of the data, it is guaranteed that 1N1001 \leq N \leq 100.

For 50%50\% of the data, it is guaranteed that there are no cycles.

For 100%100\% of the data, it is guaranteed that 1K,si,pi,viN5×1031 \le K, s_i, p_i, v_i \le N \le 5\times 10^3, 0R5×1030 \leq R \leq 5 \times 10^3. All si|s_i| are pairwise distinct, and for each si|s_i|, all pip_i are pairwise distinct, and all viv_i are pairwise distinct.

Translated by ChatGPT 5