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
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
#define ld long double
using namespace std;
const int N = 5e5+1;
bitset<N>bylo;
int depth[N][2],k,n1,total,x = 0,npoprz;
multiset<int>MS;

void assign_greedy(int depth){
    auto it = MS.lower_bound(depth);
    if(*it > depth)it--;
    if(*it == -1)total++;
    else MS.erase(MS.find(*it));
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    MS.insert(-1);
    MS.insert(N);
    cin >> k >> n1;
    total = n1,npoprz = n1;
    for(int i = 1;i <= n1;i++)depth[i][x] = 1;
    x = !x;
    for(int i = 2;i <= k;i++){
        int ni;
        cin >> ni;
        vector<int> day(ni+1);
        for(int j = 1;j <= npoprz;j++)bylo[j] = 0;
        for(int j = 1;j <= ni;j++){
            cin >> day[j];
            if(day[j] == 0)depth[j][x] = i;
            else{
                depth[j][x] = depth[day[j]][!x];
                bylo[day[j]] = 1;
            }
        }
        for(int j = 1;j <= npoprz;j++)
            if(!bylo[j])
                MS.insert(i);
        for(int j = 1;j <= npoprz;j++)bylo[j] = 0;
        for(int j = 1;j <= ni;j++){
            if(day[j] == 0)assign_greedy(i);
            else if(bylo[day[j]])assign_greedy(depth[j][x]);
            bylo[day[j]] = 1;
        }
        npoprz = ni,x = !x;
    }
    cout << total;
}