import math n = int(input()) G = [] for i in range(n): t = list(map(int, input().split())) t = [t[i] - 1 for i in range(1, len(t))] # print(t) G.append(t) # print(G) T = [[] for i in range(n)] V = [0 for i in range(n)] wynik = [(1, 1) for i in range(n)] def compute(v, out): l = 1 for (t, _) in v: l = math.lcm(l, t) suma = 0 for (t, s) in v: suma += s * (l // t) x = out // math.gcd(suma, out) suma *= x l *= x return (l, suma // out) V[0] = 1 T[0] = [(1, 1)] for i in range(n): if V[i]: wynik[i] = compute(T[i], max(1, len(G[i]))) for t in G[i]: V[t] = 1 T[t].append(wynik[i]) # print(wynik) # print(T) res = 1 for (l, _) in wynik: res = math.lcm(res, l) print(res)
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 | import math n = int(input()) G = [] for i in range(n): t = list(map(int, input().split())) t = [t[i] - 1 for i in range(1, len(t))] # print(t) G.append(t) # print(G) T = [[] for i in range(n)] V = [0 for i in range(n)] wynik = [(1, 1) for i in range(n)] def compute(v, out): l = 1 for (t, _) in v: l = math.lcm(l, t) suma = 0 for (t, s) in v: suma += s * (l // t) x = out // math.gcd(suma, out) suma *= x l *= x return (l, suma // out) V[0] = 1 T[0] = [(1, 1)] for i in range(n): if V[i]: wynik[i] = compute(T[i], max(1, len(G[i]))) for t in G[i]: V[t] = 1 T[t].append(wynik[i]) # print(wynik) # print(T) res = 1 for (l, _) in wynik: res = math.lcm(res, l) print(res) |