import fractions import math n = int(input()) dp = [fractions.Fraction(0)] * (n + 1) dp[1] = fractions.Fraction(1) for i in range(1, n + 1): L = list(map(int, input().split(' '))) r = L[0] if r > 0: for c in L[1:]: dp[c] += dp[i] * fractions.Fraction(1, r) ans = 1 for f in dp: ans = math.lcm(ans, f.denominator) print(ans)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import fractions import math n = int(input()) dp = [fractions.Fraction(0)] * (n + 1) dp[1] = fractions.Fraction(1) for i in range(1, n + 1): L = list(map(int, input().split(' '))) r = L[0] if r > 0: for c in L[1:]: dp[c] += dp[i] * fractions.Fraction(1, r) ans = 1 for f in dp: ans = math.lcm(ans, f.denominator) print(ans) |