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
#include <bits/stdc++.h>
using namespace std;
vector <int> G[103];
int ktory[103];
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int r;
        cin >> r;
        while (r--) {
            int x;
            cin >> x;
            G[i].push_back(x);
        }
    }
    int ile = 0; 
    while (true) {
        ile++;
        int v = 1;
        while (G[v].size() > 0) {
            int x = v;
            v = G[v][ktory[v]];
                ktory[x]++;
            if (ktory[x] >= G[x].size()) 
                ktory[x] = 0;
        }
        int policz = 0;
        for (int i = 1; i <= n; i ++) {
            if (ktory[i] == 0)
                policz++;
        }
        if (policz == n)
            break;
    }
    cout << ile;
    return 0;
}