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
import math

n = int(input())
outdegree = [0]*n
graf = []
dp = [0]*n
dp2 = [0]*n
for i in range(n):
    graf.append([])

for i in range(n):
    arr = [int(x) for x in input().split()]
    outdegree[i] = arr[0] 
    for k in range(outdegree[i]):
        a = arr[k+1]
        a-=1
        (graf[a]).append(i)
    

dp[0] = outdegree[0]
if outdegree[0]==0:
    print(1)
    quit()

dp2[0] = 1
maksi = dp[0] 
for i in range(1, n):
    roz = 1;
    if int(outdegree[i])==0:
        dp[i]=1
        dp2[i] = 0
        continue

    if len(graf[i])==0:
        dp[i]=1
        dp2[i] = 0
        continue

    for it in graf[i]:
        roz = math.lcm(roz, dp[it])

    suma = 0;
    for it in graf[i]:
        suma += (roz//dp[it])*dp2[it];

    if suma == 0:
        dp[i]=1
        dp2[i] = 0
        continue

    nw = math.lcm(suma, outdegree[i])
    dp2[i] = nw//outdegree[i]
    dp[i] = (nw//suma)*roz
    maksi = math.lcm(maksi, dp[i]); 

print(int(maksi))