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


vector<int> G[500'005];
vector<int> DP[500'005];

int k, n1;

int main() {
    iostream::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    cin >> k >> n1;
    G[0].resize(n1 + 1);
    DP[0].resize(n1 + 1);
    
    for (int i = 1; i < k; ++i) {
        int q; cin >> q;
        G[i].resize(q + 1);
        DP[i].resize(q + 1);

        for (int j = 1; j <= q; ++j) {
            cin >> G[i][j];
            DP[i][j] = 0;
        }
    }

    long long max_output = 0;
    for (int i = k - 1; i >= 0; --i) {
        long long curr_output = 0;
        for (int j = 1; j < G[i].size(); ++j) {
            if (DP[i][j] == 0) {
                DP[i][j] = 1;
                ++curr_output;
            }
            else {
                curr_output += DP[i][j];
            }
            if (G[i][j] != 0) {
                DP[i - 1][G[i][j]] += DP[i][j];
            }
        }
        max_output = max(max_output, curr_output);
    }

    cout << max_output << endl;

    return 0;
}