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
#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = (int)a; i <= (int)b; ++i)
#define RFOR(i, a, b) for(int i = (int)a; i >= (int)b; --i)
#define in insert
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
using namespace std;

const int MAX = 1e6 + 7;
int tp[MAX], wyn[MAX];
vector<int> g[MAX];

void solve() {
    int k, n1; cin >> k >> n1; int idx = 0;
    vector<vector<int>> nums;
    vector<int> lol;
    FOR(i, 1, n1) {
        tp[i] = ++idx;
        lol.pb(idx);
    }
    nums.pb(lol);
    FOR(i, 1, k - 1) {
        int n; cin >> n;
        vector<int> rn(n + 1);
        vector<int> lcb;    
        FOR(j, 1, n) rn[j] = ++idx;
        FOR(j, 1, n) {
            int x; cin >> x;
            lcb.pb(rn[j]);
            if(x == 0) continue;
            g[tp[x]].pb(rn[j]);
        }
        FOR(j, 1, n) tp[j] = rn[j];
        nums.pb(lcb);
    }
    int res = 0;
    RFOR(i, k - 1, 0) {
        vector<int> akt = nums[i];
        RFOR(j, akt.size() - 1, 0) {
            if(g[akt[j]].empty()) ++wyn[idx];
            else {
                FOR(y, 0, g[akt[j]].size() - 1) wyn[idx] += wyn[g[akt[j]][y]];
            }
            --idx;
        }
        int sum = 0;
        RFOR(j, akt.size() - 1, 0) sum += wyn[akt[j]];
        res = max(res, sum);
    }
    cout << res;
}   

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    solve();
    return 0;
}