def gcd(x,y): if x==0: return y return gcd(y%x,x) n = int(input()) e = [[]]*n ein = [] for i in range(n): ein.append([]) l = [0]*n for i in range(n): tmp = input() l[i] = int(tmp.split(' ',1)[0]) if l[i]>0: e[i] = [int(j)-1 for j in tmp.split(' ',1)[1].split(' ')] for v in e[i]: ein[v].append(i) if l[0]==0: print(1) else: mv = [0]*n ansb = [0]*n ans = l[0] mv[0] = 1 ansb[0] = 1 for i in range(1,n): if l[i]==0: continue ansb[i] = ans for v in ein[i]: mv[i]+=mv[v]*ans//(l[v]*ansb[v]) ans *= l[i]//gcd(mv[i],l[i]) print(ans)
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 | def gcd(x,y): if x==0: return y return gcd(y%x,x) n = int(input()) e = [[]]*n ein = [] for i in range(n): ein.append([]) l = [0]*n for i in range(n): tmp = input() l[i] = int(tmp.split(' ',1)[0]) if l[i]>0: e[i] = [int(j)-1 for j in tmp.split(' ',1)[1].split(' ')] for v in e[i]: ein[v].append(i) if l[0]==0: print(1) else: mv = [0]*n ansb = [0]*n ans = l[0] mv[0] = 1 ansb[0] = 1 for i in range(1,n): if l[i]==0: continue ansb[i] = ans for v in ein[i]: mv[i]+=mv[v]*ans//(l[v]*ansb[v]) ans *= l[i]//gcd(mv[i],l[i]) print(ans) |