n = int(input()) nbh = {} for i in range(1, n + 1): nbh[i] = [] row = input().split(' ') r = int(row[0]) for j in row[1:]: nbh[i] += [int(j)] counters = [0]*(n + 1) it = 0 while 1: v = 1 while len(nbh[v]) != 0: counters[v] += 1 counters[v] %= len(nbh[v]) v = nbh[v][counters[v]] it += 1 if sum(counters) == 0: break print(it)
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 | n = int(input()) nbh = {} for i in range(1, n + 1): nbh[i] = [] row = input().split(' ') r = int(row[0]) for j in row[1:]: nbh[i] += [int(j)] counters = [0]*(n + 1) it = 0 while 1: v = 1 while len(nbh[v]) != 0: counters[v] += 1 counters[v] %= len(nbh[v]) v = nbh[v][counters[v]] it += 1 if sum(counters) == 0: break print(it) |