import math n = int(input()) g = [0] * n gr = [0] * n d = [0] * n for i in range(n): gr[i] = list() for i in range(n): g[i] = list(map(int, input().split())) d[i] = g[i][0] for j in range(1, len(g[i])): gr[g[i][j] - 1].append(i) reset = [0] * n cnt = [0] * n reset[0] = max(d[0], 1) cnt[0] = reset[0] for i in range(1, n): s = 0 for v in gr[i]: s += reset[i - 1] // reset[v] * cnt[v] // d[v] if d[i] == 0 or s == 0: reset[i] = reset[i - 1] cnt[i] = 0 else: f = d[i] // math.gcd(d[i], s) reset[i] = reset[i - 1] * f cnt[i] = s * f print(reset[n - 1])
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 | import math n = int(input()) g = [0] * n gr = [0] * n d = [0] * n for i in range(n): gr[i] = list() for i in range(n): g[i] = list(map(int, input().split())) d[i] = g[i][0] for j in range(1, len(g[i])): gr[g[i][j] - 1].append(i) reset = [0] * n cnt = [0] * n reset[0] = max(d[0], 1) cnt[0] = reset[0] for i in range(1, n): s = 0 for v in gr[i]: s += reset[i - 1] // reset[v] * cnt[v] // d[v] if d[i] == 0 or s == 0: reset[i] = reset[i - 1] cnt[i] = 0 else: f = d[i] // math.gcd(d[i], s) reset[i] = reset[i - 1] * f cnt[i] = s * f print(reset[n - 1]) |