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
#include <iostream>
#include <vector>
using namespace std;

vector<int> input[600000], weigth[600000];

int main() {
    ios_base::sync_with_stdio(0);

    int k, n, nn, tmp;

    cin >> k >> n;

    input[1].push_back(0);
    weigth[1].push_back(0);
    for (int i = 1; i <= n; i++) {
        input[1].push_back(0);
        weigth[1].push_back(0);
    }

    for (int i = 2; i <= k; i++ ) {
        cin >> nn;
        input[i].push_back(0);
        weigth[i].push_back(0);
        for (int j = 1; j <= nn; j++) {
            cin >> tmp;
            input[i].push_back(tmp);
            weigth[i].push_back(0);
        }
    }

    int best = 0;
    for (int i = k; i > 0; i--) {
        int current = 0;
        // cout << "DAY " << i << endl;
        for (int j = 1; j < input[i].size(); j++) {
            int val = input[i][j];
            weigth[i][j] = max(1, weigth[i][j]);
            if (val) {
                weigth[i-1][val]+= weigth[i][j];
            }
            current  += weigth[i][j];
            // cout << weigth[i][j] << " ";
        }
        best = max(best, current);
        // cout << endl << current << endl;
    }

    cout << best << endl;
}