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
#include<bits/stdc++.h>
using namespace std;
typedef pair < int, int > pii;
const int logg = 32, lim = 1000'003;

pii tab[lim];
vector < int > v[lim];
vector < int > v2[lim];
int odp[lim];
bool odw[lim];

int dfs(int node, int parent){
    odw[node] = 1;
    int wyn = 1, wart = 0;
    for(int i: v2[node]){
        if(i != parent){
            wart = -1;
            wyn += dfs(i, node);
        }
    }
    wyn += wart;
    odp[tab[node].second] += wyn;
    // cout << node << ": " << wyn << "\n";
    return wyn;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int k, cnt = 1;
    cin >> k;
    v[0].push_back(0);
    for(int i = 1; i <= k; i++){
        int n;
        cin >> n;
        if(i != 1){
            for(int j = 1; j <= n; j++){
                int x;
                cin >> x;
                if(x != 0){
                    v2[v[i - 1][x - 1]].push_back(cnt);
                    v2[cnt].push_back(v[i - 1][x - 1]);
                }
                tab[cnt] = {j, i};
                v[i].push_back(cnt++);
            }
        }
        else {
            for(int j = 1; j <= n; j++){
                tab[cnt] = {j, i};
                v[i].push_back(cnt++);
            }
        }
    }
    for(int i = 1; i < cnt; i++){
        if(!odw[i])dfs(i, -1);
    }
    int ma = 0;
    for(int i = 1; i <= k; i++){
        ma = max(ma, odp[i]);
    }
    cout << ma << "\n";
}