import math from fractions import Fraction n = int(input()) g = [None] * n gr = [None] * n p = [None] * n p[0] = Fraction(1) for i in range(n): gr[i] = [] for i in range(n): g[i] = [int(x)-1 for x in input().split()] del g[i][0] for j in g[i]: gr[j].append(i) result = 1 for i in range(n): if (i != 0): p[i] = Fraction(0) for j in gr[i]: p[i] += p[j] if len(g[i]) > 0: p[i] /= len(g[i]) result = math.lcm(result, p[i].denominator) print(result)
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 | import math from fractions import Fraction n = int(input()) g = [None] * n gr = [None] * n p = [None] * n p[0] = Fraction(1) for i in range(n): gr[i] = [] for i in range(n): g[i] = [int(x)-1 for x in input().split()] del g[i][0] for j in g[i]: gr[j].append(i) result = 1 for i in range(n): if (i != 0): p[i] = Fraction(0) for j in gr[i]: p[i] += p[j] if len(g[i]) > 0: p[i] /= len(g[i]) result = math.lcm(result, p[i].denominator) print(result) |