#P11155. 【MX-X6-T1】 Subtask Dependency

【MX-X6-T1】 Subtask Dependency

Background

Original problem link: https://oier.team/problems/X6B.

Problem Description

There is an OI problem. This problem has nn subtasks, numbered from 11 to nn. Subtask ii depends on did_i subtasks, whose numbers are ai,1,ai,2,,ai,dia_{i,1},a_{i,2},\dots,a_{i,d_i}. It is guaranteed that for all 1in,1jdi1\leq i\leq n,1\leq j\leq d_i, we have ai,j<ia_{i,j}<i, i.e., a subtask only depends on subtasks with smaller numbers than itself. Note that a subtask may depend on no subtasks.

A contestant's program can be described as a length-nn 0/10/1 sequence p1,p2,,pnp_1,p_2,\dots,p_n, indicating whether the program can pass subtask ii.

When judging a program, the judge will consider each subtask in increasing order of its number. When considering subtask ii:

  • If among the subtasks it depends on, there exists a subtask whose status is wrong, then the status of subtask ii is wrong.
  • Otherwise, the judge tests this subtask. If the contestant's program can pass this subtask, then the status of this subtask is correct; otherwise, it is wrong.

The score of a program is the number of subtasks whose final status is correct after judging.

Now there are mm contestants who submitted programs. You need to compute their scores separately.

Input Format

The first line contains an integer nn, the number of subtasks.

The next nn lines: on the ii-th line, first input an integer did_i, the number of subtasks that subtask ii depends on; then input did_i integers ai,1,ai,2,,ai,dia_{i,1},a_{i,2},\dots,a_{i,d_i}, the indices of the dependent subtasks. It is guaranteed that all ai,j<ia_{i,j}<i.

The next line contains an integer mm, the number of contestant programs.

The next mm lines: each line contains nn integers. On the ii-th line, the jj-th integer pi,jp_{i,j} is 00 meaning the ii-th contestant's program cannot pass subtask jj, and 11 meaning it can pass subtask jj.

Output Format

Output mm lines. On the ii-th line, output the score of the ii-th contestant.

3
0
1 1
1 2
3
1 1 1
1 0 1
0 0 0

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

Hint

Sample Explanation #1

  • Contestant 1's program can pass all subtasks, so the results of all 33 subtasks are correct.
  • Contestant 2's program cannot pass subtask 22, so subtask 22 will be judged as wrong. Since subtask 33 depends on subtask 22, even if contestant 2's program can pass subtask 33, subtask 33 will still be judged as wrong. This contestant's judging result has only subtask 11 correct.
  • Contestant 3's program cannot pass any subtask, so the results of all subtasks are wrong.

Constraints

For all testdata, 1n,m1001\leq n,m\leq 100, 0di<i0\leq d_i<i, 1ai,j<i1\leq a_{i,j}<i, 0pi,j10\leq p_{i,j}\leq 1, and there do not exist ii and j1j2j_1\neq j_2 such that ai,j1=ai,j2a_{i,j_1}=a_{i,j_2}.

There are 1010 test groups in total, with the specific limits as follows:

  • For test groups 1,21,2, n=1n=1.
  • For test groups 3,4,53,4,5, for all ii, di1d_i\leq 1.
  • For the other test groups, there are no additional constraints.

Translated by ChatGPT 5