#!/usr/bin/env python3 from fractions import Fraction from math import gcd n = int(input()) edges = [] for _ in range(n): l = list(map(lambda s: int(s)-1, input().strip().split())) assert len(l) == l[0] + 2 edges.append(l[1:]) freqact = [Fraction(0,1)]*n freq_reset = [Fraction(0,1)]*n freqact[0] = Fraction(1,1) nww = 1 for i in range(n): out_edges = len(edges[i]) if out_edges > 0: freq_reset[i] = freqact[i] * Fraction(1, out_edges) if out_edges > 1: nww = nww // gcd(nww, freq_reset[i].denominator) * freq_reset[i].denominator for e in edges[i]: freqact[e] += freq_reset[i] print(nww)
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 | #!/usr/bin/env python3 from fractions import Fraction from math import gcd n = int(input()) edges = [] for _ in range(n): l = list(map(lambda s: int(s)-1, input().strip().split())) assert len(l) == l[0] + 2 edges.append(l[1:]) freqact = [Fraction(0,1)]*n freq_reset = [Fraction(0,1)]*n freqact[0] = Fraction(1,1) nww = 1 for i in range(n): out_edges = len(edges[i]) if out_edges > 0: freq_reset[i] = freqact[i] * Fraction(1, out_edges) if out_edges > 1: nww = nww // gcd(nww, freq_reset[i].denominator) * freq_reset[i].denominator for e in edges[i]: freqact[e] += freq_reset[i] print(nww) |