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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
#include <bits/stdc++.h>
#define ll long long
#define llu long long unsigned
#define ld long double
#define fr(i,n) for(int i=0;i<n;i++)
#define watch(x) cout<<(#x)<<"=="<<(x)<<endl
#define ft first
#define sc second
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define P 1000000007llu
#define N 500005
#define LC 262144
using namespace std;

int getchar_pos_int() {
    int n=0, c=getchar();
    while('0'<=c&&c<='9') {n=n*10+c-'0'; c=getchar();}
    return n;
}

int k,n[N],x,nv, leafCount[N],curr, res=0,leftover;
vi a[N],adj[N],lc2[N];
vector<bool> hasParent2[N],hasDesc2[N];
bool hasDesc[N],hasParent[N];

int leafCountDFS(int src) {
    if (adj[src].empty()) leafCount[src]=1;
    else for (int v: adj[src]) leafCount[src]+=leafCountDFS(v);
   // watch(src);watch(leafCount[src]);
    //for (int v: adj[src]) watch(v);
    return leafCount[src];
}
// TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
int main() {
    k=getchar_pos_int(),nv=n[0]=getchar_pos_int();
    fr(i,nv) hasDesc2[0].pb(false);
    fr(i,nv) hasParent2[0].pb(false);
    for (int i=1;i<k;i++) {
        //watch(i);
        n[i]=getchar_pos_int();
        fr(i,nv) hasDesc2[i].pb(false);
        fr(j,n[i]) {
            x=getchar_pos_int();
            a[i].pb(x);
            //watch(x);
            if (x) {
                int par=nv-n[i-1]+x-1;
                //watch(par);
                adj[par].pb(nv+j);
                hasDesc2[i-1][x-1]=hasDesc[par]=hasParent[nv+j]=true;
                hasParent2[i].pb(true);
            } else hasParent2[i].pb(false);
        }
        nv+=n[i];
    }
    /*fr(i,nv) {
        watch(i);
        for(int v: adj[i]) watch(v);
    }*/
    fr(i,nv) if (!hasParent[i]) leafCountDFS(i);
    {
        int j=0;
        fr(i,k) fr(l,n[i]) {
            lc2[i].pb(leafCount[j++]);
        }
    }
    fr(i,k) {
        curr=0;
        fr(j,n[i]) if (!hasParent2[i][j]) curr+=lc2[i][j];
        if (i) {
            if (leftover>curr) {
                leftover-=curr, curr=0;
            } else curr-=leftover,leftover=0;
        }
        for(bool x: hasDesc2[i]) if (!x) {leftover++;}
        res+=curr;
        //watch(curr); watch(res); watch(leftover);
    }
  /*  fr(i,k) {
        watch(i);
        fr(j,n[i]) {watch(j);watch(lc2[i][j]);}
    }
    fr(i,k) {
        fr(l,n[i]) {
            watch(lc2[i][l]);
        }
    }*/
    printf("%d\n",res);
    // TIP See CLion help at <a href="https://www.jetbrains.com/help/clion/">jetbrains.com/help/clion/</a>. Also, you can try interactive lessons for CLion by selecting 'Help | Learn IDE Features' from the main menu.
}

/*
4 3
3 1 1 1
4 0 0 2 0
2 3 3
*/