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

const int MAX_K = 500005;
const int MAX_NODES_COUNT = 500005;

int pref_sum[MAX_K];
vector<int> graph[MAX_NODES_COUNT];
int min_attendance_cache[MAX_NODES_COUNT];

int to_node_id(int day, int indx) {
    return pref_sum[day] + indx;
}

int get_min_attendance(int node_id) {
    if (min_attendance_cache[node_id] != 0) {
        return min_attendance_cache[node_id];
    }
    if (graph[node_id].empty()) {
        return min_attendance_cache[node_id] = 1;
    }
    for (int child_id : graph[node_id]) {
        min_attendance_cache[node_id] += get_min_attendance(child_id);
    }
    return min_attendance_cache[node_id];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int k, n, a;
    cin >> k >> n;
    pref_sum[1] = n;
    for (int i = 1; i <= k; i++) {
        cin >> n;
        pref_sum[i + 1] = pref_sum[i] + n;
        for (int j = 1; j <= n; j++) {
            cin >> a;
            if (a > 0) {
                int parent_id = to_node_id(i - 1, a);
                graph[parent_id].push_back(to_node_id(i, j));
            }
        }
    }
    int res = 0;
    for (int i = 1; i <= k; i++) {
        int curr_min = 0;
        for (int j = pref_sum[i - 1] + 1; j <= pref_sum[i]; j++) {
            curr_min += get_min_attendance(j);
        }
        res = max(res, curr_min);
    }
    cout << res << endl;
    return 0;
}