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
#include <vector>
#include <iostream>

using namespace std;

typedef long long int ll;


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int k, n;

    cin >> k >> n;

    vector<vector<int>> graph;
    vector<vector<int>> count;

    graph.push_back(vector<int>(n, 0));
    count.push_back(vector<int>(n, 0));

    if (k == 1){
        cout << n << endl;
        return 0;
    }

    for(int i = 1; i < k; i++){
        cin >> n;
        count.push_back(vector<int>(n, 0));
        graph.push_back(vector<int>());

        for (int j = 0; j < n; j++){
            int edge;
            cin >> edge;
            graph.back().push_back(edge);
        }
    }

    for(size_t i = 0; i < graph.back().size(); i++){
        count.back()[i] = 1;
    }
    int ans = graph.back().size();

    for(size_t i = k - 1; i >= 1; i--){
        for(size_t j = 0; j < count[i].size(); j++){
            if(graph[i][j] > 0){
                count[i-1][graph[i][j] - 1] += count[i][j];
            }
        }

        int current_count = 0;
        for(size_t j = 0; j < count[i-1].size(); j++){
            if(count[i-1][j] <= 0) count[i-1][j] = 1;
            current_count += count[i-1][j];
        }
        ans = max(ans, current_count);
    }

    cout << ans << endl;
    return 0;
}