import math def Divide(t, n): a = t[0] b = t[1] * n g = math.gcd(a, b) a = a // g b = b // g return (a, b) def Add(f1, f2): a = f1[0] * f2[1] b = f2[0] * f1[1] c = f2[1] * f1[1] a = a + b g = math.gcd(a, c) a = a // g c = c // g return (a, c) n = int(input()) cnt = {} for i in range(n): cnt[i] = (0, 1) cnt[0] = (1, 1) ans = 1 for i in range(n): adj = list(map(int, input().strip().split(' ')))[1:] num_adj = len(adj) if num_adj == 0: continue cnt[i] = Divide(cnt[i], num_adj) for x in adj: cnt[x - 1] = Add(cnt[x - 1], cnt[i]) g = math.gcd(ans, cnt[i][1]) ans = ans // g ans = ans * cnt[i][1] print(ans)
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 | import math def Divide(t, n): a = t[0] b = t[1] * n g = math.gcd(a, b) a = a // g b = b // g return (a, b) def Add(f1, f2): a = f1[0] * f2[1] b = f2[0] * f1[1] c = f2[1] * f1[1] a = a + b g = math.gcd(a, c) a = a // g c = c // g return (a, c) n = int(input()) cnt = {} for i in range(n): cnt[i] = (0, 1) cnt[0] = (1, 1) ans = 1 for i in range(n): adj = list(map(int, input().strip().split(' ')))[1:] num_adj = len(adj) if num_adj == 0: continue cnt[i] = Divide(cnt[i], num_adj) for x in adj: cnt[x - 1] = Add(cnt[x - 1], cnt[i]) g = math.gcd(ans, cnt[i][1]) ans = ans // g ans = ans * cnt[i][1] print(ans) |