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
#include <iostream>

using namespace std;

short platforms[101][101];

bool endCondition(short counters[], short len) {
    for(int i=1; i<=len; i++){
        if (counters[i] != 1) return false;
    }
    return true;
}

int main() {
    int platformNo;
    cin >> platformNo;

    for (int i=1; i<=platformNo; i++) {
        int beltsNo;
        cin >> beltsNo;
        platforms[i][0] = beltsNo;
        for (int j=1; j<=beltsNo; j++)
            cin >> platforms[i][j];
    }

    short counters[101];
    for(int i=0; i <= platformNo; i++) counters[i] = 1;

    // for (int i=1; i<=platformNo; i++) {
    //     cout << "platform " << i << ":";
    //     for (int j=0; j<=platforms[i][0]; j++){
    //         cout << platforms[i][j];
    //     }
    //     cout<<endl;
    // }

    long long turn = 0;
    do {
        bool caseFinished = false;
        int currentPlatform = 1;
        //cerr<<"["<<turn<<"] ";
        while(!caseFinished) {
            //cerr << currentPlatform << "->";
            if(platforms[0] == 0) {
                caseFinished = true;
                break;
            }
            short nextPlatform = platforms[currentPlatform][counters[currentPlatform]];
            counters[currentPlatform]++;
            if (counters[currentPlatform] > platforms[currentPlatform][0]) counters[currentPlatform] = 1;
            if (nextPlatform == 0) caseFinished = true;
            currentPlatform = nextPlatform;
        }
        //cerr << endl;
        turn++;
    } while (!endCondition(counters, platformNo));
    cout << turn << endl;
}