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
def gcd(a, b):
  return gcd(b,a%b) if b else a

if __name__=="__main__":
    n = int(input())
    g = []
    cnt = []
    c = []
    r = []
    for i in range(n+1):
        g.append([])
        cnt.append(0)
        r.append(0)
        c.append(0)
    for i in range(n):
        s = input().strip().split(' ')
        cnt[i+1] = int(s[0])
        for j in range(cnt[i+1]):
            g[int(s[j+1])].append(i+1)
    r[1]=cnt[1];
    c[1]=cnt[1];
    for i in range(2,n+1):
      r[i]=r[i-1];
      c[i]=0;
      if (cnt[i]==0):
        continue
      for j in g[i]:
        if c[j]!=0:
          c[i]+=(c[j]//cnt[j])*(r[i]//r[j])
      if (c[i]==0):
        continue
      cur=c[i];
      c[i]=(cnt[i]//gcd(cnt[i],cur))*cur;
      r[i]*=c[i]//cur;
    print(max(r[n],1))