from fractions import Fraction from math import lcm n = int(input()) nastepnicy = [] dlugosc = [] for _ in range(n): lista = [int(x)-1 for x in input().split()] dlugosc.append(lista[0]+1) nastepnicy.append(lista[1:]) ulamki = [Fraction(0)] * n ulamki[0] = Fraction(1) for i in range(n): for neighbour in nastepnicy[i]: ulamki[neighbour] += ulamki[i] / dlugosc[i] maximum = 1 for i in range(n): if dlugosc[i] != 0: maximum = lcm(maximum, (ulamki[i] / dlugosc[i]).denominator) print(maximum)
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 | from fractions import Fraction from math import lcm n = int(input()) nastepnicy = [] dlugosc = [] for _ in range(n): lista = [int(x)-1 for x in input().split()] dlugosc.append(lista[0]+1) nastepnicy.append(lista[1:]) ulamki = [Fraction(0)] * n ulamki[0] = Fraction(1) for i in range(n): for neighbour in nastepnicy[i]: ulamki[neighbour] += ulamki[i] / dlugosc[i] maximum = 1 for i in range(n): if dlugosc[i] != 0: maximum = lcm(maximum, (ulamki[i] / dlugosc[i]).denominator) print(maximum) |