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
import math
import sys
from collections import defaultdict

rev = defaultdict(list)

n = int(sys.stdin.readline())
deg = [0] * (n + 1)
for i in range(1, n+1):
    line = sys.stdin.readline()
    line = line.strip(' \n')
    numbers = [int(x) for x in line.split(" ")]
    deg[i] = numbers[0]
    for v in numbers[1:]:
        rev[v].append(i)

r = max(1, deg[1])
xs = [0, r]
for v in range(2, n + 1):
    x = 0

    for prev in rev[v]:
        x += xs[prev] // deg[prev]

    if x != 0:
        mult = max(1, deg[v]) // math.gcd(max(1, deg[v]), x)
        xs.append(x)
        for i in range(0, len(xs)):
            xs[i] = xs[i] * mult
        r = r * mult
    else:
        xs.append(0)

print(r)