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

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

    int k, n1;
    cin >> k >> n1;

    vector<vector<int>> days(k);
    days[0] = vector<int>(n1, 0);

    for (int i = 1; i < k; ++i) {
        int ni;
        cin >> ni;
        days[i].resize(ni);
        for (int j = 0; j < ni; ++j) cin >> days[i][j];
    }

    vector<vector<ll>> needed(k);

    for (int day = k-1; day >= 0; --day) {
        int ni = days[day].size();
        needed[day].assign(ni, 0);

        if (day == k-1) {
            for (int j = 0; j < ni; ++j) needed[day][j] = 1;
        } else {
            int next_ni = days[day+1].size();
            vector<vector<int>> cont_map(ni);
            for (int idx = 0; idx < next_ni; ++idx) {
                int val = days[day+1][idx];
                if (val > 0) cont_map[val-1].push_back(idx);
            }
            for (int j = 0; j < ni; ++j) {
                if (!cont_map[j].empty()) {
                    ll sum = 0;
                    for (int idx : cont_map[j]) sum += needed[day+1][idx];
                    needed[day][j] = sum;
                } else {
                    needed[day][j] = 1;
                }
            }
        }
    }

    ll result = 0;
    for (ll x : needed[0]) result += x;
    cout << result << "\n";

    return 0;
}