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
#include <bits/stdc++.h>

using namespace std;
const int MX = 1e6+9;
vector<int>graph[MX], level[MX];
int par[MX], in[MX], it = 1;
int get(){
    int x = 0;
    char c = getchar_unlocked();
    while(c < '0' || c > '9') c = getchar_unlocked();
    while(c >= '0' && c <= '9'){
        x = x*10+c-'0';
        c = getchar_unlocked();
    }
    return x;
}
int dfs(int vert){
    if(graph[vert].empty()) return 1;
    int sum = 0;
    for(auto u : graph[vert]) sum += dfs(u);
    return sum;
}
int main(){
    int D = get(), n1 = get();
    level[0].push_back(0);
    level[1].push_back(0);
    for(auto i = 1; i <= n1; ++i) level[1].push_back(it++);
    for(auto d = 2; d <= D; ++d){
        int n = get();
        level[d].push_back(0);
        for(auto i = 1; i <= n; ++i){
            int p = get();
            par[it] = level[d-1][p];
            graph[par[it]].push_back(it);
            level[d].push_back(it++);
        }
    }
    int cur = 0, res = 0;
    for(auto d = 1; d <= D; ++d){
        for(auto vert : level[d]){
            if(!vert || par[vert]) continue;
            cur += dfs(vert);
        }
        res = max(res, cur);
        for(auto vert : level[d]){
            if(!vert || graph[vert].size()) continue;
            cur--;
        }
        // cout << "    " << cur << endl;
    }
    cout << res;
}