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
#include <bits/stdc++.h>
using namespace std;


int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int k; cin >> k;

    vector<vector<int>> N, P;

    int n; cin >> n;
    N.push_back(vector<int>(n, 0)); P.push_back(vector<int>(n, 0));

    for (int i=1; i < k; i++) {
        cin >> n;
        P.push_back(vector<int>(n, 0));
        vector<int> a(n);
        for (int j=0; j<n; j++) {
            cin >> a[j];
        }
        N.push_back(a);
    }

    for (int i=k-1; i>0; i--) {
        for (int j=0; j<N[i].size(); j++) {
            if (P[i][j] == 0) {
                P[i][j] = max(P[i][j], 1);
            }
            if (N[i][j] != 0) {
                P[i-1][N[i][j]-1] += P[i][j];
            }
        }
    }
    for (int j=0; j<N[0].size(); j++) {
        P[0][j] = max(P[0][j], 1);
    }

    int ans = 0;

    for (auto v: P) {
        int sum = 0;
        for (auto x: v) {
            sum += x;
        }
        ans = max(ans, sum);
    }

    cout << ans << "\n";

    return 0;
}