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
#include <bits/stdc++.h>
using namespace std;
struct Node{
    bool visited;
    vector<pair<int,int>>pol;
    int day;
    Node(int d){
        this->day = d;
        this->visited = false;
    }
};
void DFS(int i,int j,vector<vector<Node>>&v,priority_queue<int,vector<int>,greater<int>>&q,int begin){
    v[i][j].visited=true;
    if(v[i][j].pol.size()==0){
        if(q.empty()==false && q.top()<=begin){
            q.pop();
        }
        q.push(v[i][j].day+1);
    }
    else{
        for(int k=0;k<v[i][j].pol.size();k++){
            pair<int,int>p = v[i][j].pol[k];
            DFS(p.first,p.second,v,q,begin);
        }
    }
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    vector<vector<Node>>v;
    int k,n_1;
    cin>>k>>n_1;
    for(int i=0;i<k;i++){
        vector<Node>wyp;
        v.push_back(wyp);
    }
    for(int i=0;i<n_1;i++){
        Node np(0);
        v[0].push_back(np);
    }
    for(int i=1;i<k;i++){
        int n_i;
        cin>>n_i;
        for(int j=0;j<n_i;j++){
            int nr;
            cin>>nr;
            Node np(i);
            v[i].push_back(np);
            if(nr!=0){
                v[i-1][nr-1].pol.push_back({i,j});
            }
        }
    }
    priority_queue<int,vector<int>,greater<int>>q;
    for(int i=0;i<v.size();i++){
        for(int j=0;j<v[i].size();j++){
            if(!v[i][j].visited){
                DFS(i,j,v,q,v[i][j].day);
            }
        }
    }
    cout<<q.size()<<'\n';
}