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
#include <cstdio>
#include <vector>
#define FOR(i,a,b) for(int i=(int)(a); i<(int)(b); ++i)
using namespace std;

int k, n;
vector<vector<int> > A;  // Continuation ID (from task data)
vector<vector<int> > R;  // R[i][j] = number of people we need on Meeting A[i][j]
int main() {

    // Read input
    scanf("%d %d", &k, &n);
    A.resize(k); R.resize(k);
    A[0].resize(n, 0); R[0].resize(n, 0);
    FOR(i,1,k) {
        scanf("%d", &n);
        A[i].resize(n); R[i].resize(n, 0);
        FOR(j,0,n) scanf("%d", &A[i][j]);
    }

    // Go through days backward
    int available = 0;
    FOR(i,0,k) {
        int day = k-i-1;

        // Ensure that at current day every meeting has at least 1 participant
        FOR(j,0,R[day].size()) {
            if (R[day][j] == 0){
                R[day][j] = 1;
                if (available > 0) --available;
            }
        }

        // Move all participants to day "i+1"
        FOR(j,0,A[day].size()) {
            int a = A[day][j];
            if (a > 0) {
                R[day-1][a-1] += R[day][j];
            } else {
                available += R[day][j];
            }
        }
    }

    // Print result
    printf("%d\n", available);
    return 0;
}