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

using namespace std;

int main(){
    vector<vector<int>> sasiedzi;
    vector<int> iteratory;
    int n;
    int stopien;
    int sasiad;

    cin >> n;
    for(int i = 0; i < n; i++){
        iteratory.push_back(0);
        cin >> stopien;
        vector<int> wierzcholek;
        if(stopien > 0){
            for(int j = 0; j < stopien; j++){
                cin >> sasiad;
                sasiad--;
                wierzcholek.push_back(sasiad);
            }
        }
        sasiedzi.push_back(wierzcholek);
    }

    vector<vector<int>>::iterator it = sasiedzi.begin();
    if(it->empty())
        cout << 1 << endl;
    else{

    int flag;
    int sum = 0;
    do{
        flag = 1;
        vector<int>::iterator I = iteratory.begin();
        it = sasiedzi.begin();
        vector<int>::iterator w;
        while (it->size() > 0)
        {
            w = it->begin();
            w = w + *I;
            if(*I + 1 >=(int) it->size())
                *I = 0;
            else
                *I = *I + 1;
            I = iteratory.begin();
            I = I + *w;
            it = sasiedzi.begin();
            it = it + *w;
        }
        I = iteratory.begin();
        while (I != iteratory.end())
        {
            if(*I > 0){
                flag = 0;
                break;
            }
            I++;
        }
        sum++;
    } while(flag == 0);
    
    cout << sum << endl;
    }
}