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>
using namespace std;

const int N = 5e5+5;
vector <vector<int>> waros;
vector <int> graf[N];
int wart[N], poz[N], sumpoz[N], ak=1;
bool vis[N];

void dfs( int x ){
    if( vis[x] )
        return ;
    // cerr << "IN " << x << '\n';
    vis[x]=1;
    wart[x]=0;
    for( int i:graf[x] ){
        dfs(i);
        wart[x] += wart[i];
    }
    if( wart[x] == 0 )
        wart[x] = 1; 
    // cerr << " waros " << x << ' ' << wart[x] << '\n';
    sumpoz[poz[x]] += wart[x];
    return;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int iledni, ilespo, dok, odp=0;
    cin >> iledni >> ilespo;
    waros.push_back({});
    for( int i=0; i<ilespo; ++i ){
        wart[ak]=1;
        poz[ak]=0;
        waros[0].push_back(ak);
        ak++;
    }
    for( int i=1; i<iledni; ++i ){
        waros.push_back({});
        cin >> ilespo;
        for( int j=0; j<ilespo; ++j ){
            cin >> dok;
            poz[ak]=waros.size()-1;
            waros.back().push_back(ak);
            ak++;
            if( dok == 0 )
                continue;
            graf[waros[waros.size()-2][dok-1]].push_back(ak-1);
        }
    }
    for( int i=1; i<ak; ++i )
        dfs(i);
    for( int i=0; i<waros.size(); ++i ){
        odp = max( odp, sumpoz[i] );
    }
    cout << odp << '\n';

    return 0;
}