def gcd(a,b): return b if a == 0 else gcd(b%a,a) def lcm(a,b): return (a//gcd(a,b)) * b n = int(input()) pop = [] deg = [0] * n order = [1] * n cnt = [0] * n for i in range(n): pop.append([]) for i in range(n): tab = [int(x) for x in input().split()] deg[i] = tab[0] for v in tab[1:]: pop[v-1].append(i) order[0] = 1 if deg[0] == 0 else deg[0] cnt[0] = 1 c_ord = order[0] for i in range(1,n): if len(pop[i]) == 0: continue s = sum([cnt[v]*(c_ord//order[v]) for v in pop[i]]) if s != 0 and deg[i] != 0: to_get = lcm(s,deg[i]) cnt[i] = to_get // deg[i] order[i] = c_ord * (to_get // s) c_ord = lcm(c_ord, order[i]) print(c_ord)
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 | def gcd(a,b): return b if a == 0 else gcd(b%a,a) def lcm(a,b): return (a//gcd(a,b)) * b n = int(input()) pop = [] deg = [0] * n order = [1] * n cnt = [0] * n for i in range(n): pop.append([]) for i in range(n): tab = [int(x) for x in input().split()] deg[i] = tab[0] for v in tab[1:]: pop[v-1].append(i) order[0] = 1 if deg[0] == 0 else deg[0] cnt[0] = 1 c_ord = order[0] for i in range(1,n): if len(pop[i]) == 0: continue s = sum([cnt[v]*(c_ord//order[v]) for v in pop[i]]) if s != 0 and deg[i] != 0: to_get = lcm(s,deg[i]) cnt[i] = to_get // deg[i] order[i] = c_ord * (to_get // s) c_ord = lcm(c_ord, order[i]) print(c_ord) |