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
#no dobrze, napisałem w pythonie, niech wam będzie
import sys
import math

def lcm(a,b):
  return a // math.gcd(a,b) * b

n = int(input())
l = [0] * n
m = [1] * n
l[0] = 1
res = 1
for i in range(n):
  arr = list(map(int,input().split()))
  if arr[0] != 0 and l[i] != 0 :
    cd = math.gcd(arr[0], l[i])
    l[i] = l[i] // cd
    m[i] *= arr[0] // cd
    res = lcm(res, m[i])
    for el in arr[1:]:
      el-=1
      nm = lcm(m[i], m[el])
      l[el] *= nm // m[el]
      m[el] = nm;
      l[el] += l[i] * (nm // m[i])
      cd = math.gcd(l[el],m[el])
      l[el] = l[el] // cd
      m[el] = m[el] // cd
print(res);