import math n = int(input()) graph = [[] for u in range(n)] r = [0 for u in range(n)] for u in range(n): tmp = [int(x) for x in input().split()] r[u] = tmp[0] for i in range(1, len(tmp)): graph[u].append(tmp[i] - 1) cnt = [0 for u in range(n)] cnt[0] = 1 ok = False while not ok: ok = True for u in range(n): if r[u] > 0: if cnt[u] % r[u] != 0: tmp = cnt[0] * r[u] // math.gcd(cnt[u], r[u]) cnt = [0 for u in range(n)] cnt[0] = tmp ok = False break to_add = cnt[u] // r[u] for v in graph[u]: cnt[v] += to_add print(cnt[0])
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 | import math n = int(input()) graph = [[] for u in range(n)] r = [0 for u in range(n)] for u in range(n): tmp = [int(x) for x in input().split()] r[u] = tmp[0] for i in range(1, len(tmp)): graph[u].append(tmp[i] - 1) cnt = [0 for u in range(n)] cnt[0] = 1 ok = False while not ok: ok = True for u in range(n): if r[u] > 0: if cnt[u] % r[u] != 0: tmp = cnt[0] * r[u] // math.gcd(cnt[u], r[u]) cnt = [0 for u in range(n)] cnt[0] = tmp ok = False break to_add = cnt[u] // r[u] for v in graph[u]: cnt[v] += to_add print(cnt[0]) |