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
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
 
using namespace std;

 
#define ll long long
#define ve vector
#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
 
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

void solve(){
    int k, n1;
    cin >> k >> n1;
    ve<ve<ve<int> > > g(k);
    ve<ve<int> > dp(k);
    ve<ve<bool> > isrt(k);
    g[0].resize(n1);
    isrt[0].assign(n1, 1);
    dp[0].assign(n1, 0);

    for(int i = 1; i < k; i++){
        int n;
        cin >> n;
        g[i].resize(n);
        isrt[i].resize(n);
        dp[i].assign(n, 0);
        for(int j = 0; j < n; j++){
            int x;
            cin >> x;
            isrt[i][j] = x == 0;
            if(!isrt[i][j]){
                g[i-1][x-1].push_back(j);
            }
        }
    }

    function<void(int, int)> dfs = [&](int i, int j){
        for(auto to : g[i][j]){
            dfs(i+1, to);
            dp[i][j] += dp[i+1][to];
        }
        if(dp[i][j] == 0)
            dp[i][j] = 1;
    };
    for(int i = 0; i < k; i++)
        for(int j = 0; j < g[i].size(); j++)
            if(isrt[i][j])
                dfs(i, j);
    int res = 0;
    for(int i = 0; i < k; i++){
        int sum = 0;
        for(auto x : dp[i])
            sum += x;
        res = max(res, sum);
    }
    cout << res << "\n";
}
 
signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int T = 1;
    // cin >> T;
    while(T--) solve();
}