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

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

    int k;
    cin >> k;

    vector<vector<int>> a(k);

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

    int result = 0;

    // liczymy zera (nowe łańcuchy)
    for (int i = 0; i < k; i++) {
        for (int x : a[i]) {
            if (x == 0) result++;
        }
    }

    // liczymy dzieci
    for (int i = 0; i < k - 1; i++) {
        int n = a[i].size();
        vector<int> children(n, 0);

        for (int j = 0; j < (int)a[i+1].size(); j++) {
            if (a[i+1][j] > 0) {
                int p = a[i+1][j] - 1;
                children[p]++;
            }
        }

        for (int j = 0; j < n; j++) {
            if (children[j] > 1) {
                result += children[j] - 1;
            }
        }
    }

    cout << result << "\n";
    return 0;
}