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

struct Platforma{
    int nTasm;
    int tasma[105];
    int biezaca;
}platfa[105];
    int n,x,ile;

void jazdaa(int i ){
    platfa[i].biezaca++;
    if(platfa[i].biezaca > platfa[i].nTasm)
        platfa[i].biezaca = 1;

    if(platfa[i].nTasm > 0){
        jazdaa( platfa[i].tasma[ platfa[i].biezaca ]);
    } else
        return;
    return;
}

bool sprawdzTasmociagi (){
    for(int i=1; i<=n; i++){
        if(platfa[i].biezaca>0 && platfa[i].biezaca<platfa[i].nTasm)
           return false;
    }
    return true;
}

int main(){
    scanf(" %d ",&n);
    for(int i=1; i<=n; i++){
        scanf(" %d ",&x);
        platfa[ i ].nTasm = x;
        platfa[ i ].biezaca = 0;
        for(int j=0; j<x; j++){
            scanf(" %d ",&platfa[ i ].tasma[ ++platfa[ i ].biezaca ]);
        }
        platfa[ i ].biezaca = 0;
    }
    jazdaa(1);
    ile = 1;
    while(!sprawdzTasmociagi()){
        jazdaa(1);
        ile++;
    }
    printf("%d\n",ile);
    return 0;
}