#!/usr/bin/python3.9 from fractions import Fraction from math import lcm from sys import exit class Tas: def __init__(self, nr, out): self.nr = nr self.out = out self.tas_list = [] self.input_list = [] self.freq = 0 n = int(input()) t = [] t.append( Tas(0,0) ) def calc_freq(nr): ts = t[nr] if nr == 1: ts.freq = Fraction(1,ts.out) else: if len(ts.input_list) == 0: return if ts.out == 0: return suma = Fraction(0) for f in ts.input_list: suma = suma + f ts.freq = suma * Fraction(1,ts.out) for tas in ts.tas_list: t[tas].input_list.append(ts.freq) for i in range(1,n+1): l = input().split(" ") k = int(l[0]) ts = Tas(i,k) for j in range(1,k+1): nr = int(l[j]) ts.tas_list.append(nr) t.append(ts) if t[1].out == 0: print("1") exit() for i in range(1,n+1): calc_freq(i) res=1 for i in range(1,n+1): if t[i].out > 1 and t[i].freq != 0: res = lcm(res, t[i].freq.denominator) print(res)
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #!/usr/bin/python3.9 from fractions import Fraction from math import lcm from sys import exit class Tas: def __init__(self, nr, out): self.nr = nr self.out = out self.tas_list = [] self.input_list = [] self.freq = 0 n = int(input()) t = [] t.append( Tas(0,0) ) def calc_freq(nr): ts = t[nr] if nr == 1: ts.freq = Fraction(1,ts.out) else: if len(ts.input_list) == 0: return if ts.out == 0: return suma = Fraction(0) for f in ts.input_list: suma = suma + f ts.freq = suma * Fraction(1,ts.out) for tas in ts.tas_list: t[tas].input_list.append(ts.freq) for i in range(1,n+1): l = input().split(" ") k = int(l[0]) ts = Tas(i,k) for j in range(1,k+1): nr = int(l[j]) ts.tas_list.append(nr) t.append(ts) if t[1].out == 0: print("1") exit() for i in range(1,n+1): calc_freq(i) res=1 for i in range(1,n+1): if t[i].out > 1 and t[i].freq != 0: res = lcm(res, t[i].freq.denominator) print(res) |