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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int k;
    cin>>k;
    vector<int>n(k+1);
    cin>>n[1];
    vector<vector<ll>>m(k+1);
    vector<vector<ll>>need(k+1);
    m[1]  = vector<ll>(n[1]+1, 0);
    need[1]=vector<ll>(n[1]+1, 0);
    for (int i=2; i<=k; i++) {
        cin>>n[i];
        m[i]=vector<ll>(n[i]+1, 0);
        need[i]=vector<ll>(n[i]+1, 0);
        for (int j=1; j<=n[i]; j++) cin>>m[i][j];
    }
    for (int i=1; i<=n[k]; i++) {
        need[k][i]=1;
    }
    ll res=0;
    ll tmp=0;
    for (int i=k; i>=2; i--) {
        tmp=0;
        for (int j=1; j<=n[i]; j++) tmp+=need[i][j];
        res=max(res,tmp);
        for (int j=1; j<=n[i]; j++) {
            if (m[i][j]!=0)
            need[i-1][m[i][j]]+=need[i][j];
        }
        for (int j=1; j<=n[i-1]; j++) {
            need[i-1][j]=max(need[i-1][j], 1ll);
        }
    }
    tmp=0;
    for (int i=1; i<=n[1]; i++) tmp+=need[1][i];
    res=max(res, tmp);
    cout<<res<<'\n';
    return 0;
}