1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <algorithm>
#include <iostream>

using namespace std;

int P[500005];
int D[500005];
int C[500005];
int NC[500005];

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int k, n1;
    cin >> k >> n1;
    D[1] = n1;
    int tot = n1;
    int ni;
    for (int i = 2; i <= k; i++) {
        cin >> ni;
        D[i] = ni;
        for (int j = 0; j < ni; j++) {
            cin >> P[tot + j];
        }
        tot += ni;
    }
    int mx = 0, cc = 0, pts = 0;
    for (int i = k; i >= 1; i--) {
        tot -= D[i];
        cc -= pts;
        for (int j = 0; j < D[i]; j++) {
            if (C[j] == 0) {
                cc++;
                C[j] = 1;
            }
        }
        mx = max(mx, cc);

        pts = 0;
        for (int j = 0; j < D[i]; j++) {
            if (P[tot + j] == 0) pts += C[j];
            if (P[tot + j] > 0) NC[P[tot + j] - 1] += C[j];
        }
        for (int j = 0; j < D[i]; j++) {
            C[j] = 0;
        }
        for (int j = 0; j < D[i]; j++) {
            if (P[tot + j] > 0) {
                C[P[tot + j] - 1] = NC[P[tot + j] - 1];
            }
        }
        for (int j = 0; j < D[i]; j++) {
            if (P[tot + j] > 0) {
                NC[P[tot + j] - 1] = 0;
            }
        }
    }
    cout << mx << "\n";
    cout.flush();

    return 0;
}