import math n=int(input()) deg=[int(0)]*(n+1) DPl=[int(0)]*(n+1) DPm=[int(0)]*(n+1) G = [[] for x in range(n+1)] for i in range(1,n+1): tab=list(map(int,input().split())) deg[i]=tab[0] for j in range(1,deg[i]+1): G[tab[j]].append(i) DPl[1]=int(1) ans=int(1) for i in range(1,n+1): DPm[i]=int(1) for j in G[i]: L=DPl[i]*DPm[j]+DPl[j]*DPm[i] M=DPm[i]*DPm[j] g=math.gcd(L,M) L=L//g M=M//g DPl[i]=L DPm[i]=M if(deg[i]>0): DPm[i]*=deg[i] g=math.gcd(DPm[i], DPl[i]) DPm[i]=int(DPm[i]//g) DPl[i]=int(DPl[i]//g) ans=int(ans*DPm[i]//math.gcd(ans,DPm[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 37 | import math n=int(input()) deg=[int(0)]*(n+1) DPl=[int(0)]*(n+1) DPm=[int(0)]*(n+1) G = [[] for x in range(n+1)] for i in range(1,n+1): tab=list(map(int,input().split())) deg[i]=tab[0] for j in range(1,deg[i]+1): G[tab[j]].append(i) DPl[1]=int(1) ans=int(1) for i in range(1,n+1): DPm[i]=int(1) for j in G[i]: L=DPl[i]*DPm[j]+DPl[j]*DPm[i] M=DPm[i]*DPm[j] g=math.gcd(L,M) L=L//g M=M//g DPl[i]=L DPm[i]=M if(deg[i]>0): DPm[i]*=deg[i] g=math.gcd(DPm[i], DPl[i]) DPm[i]=int(DPm[i]//g) DPl[i]=int(DPl[i]//g) ans=int(ans*DPm[i]//math.gcd(ans,DPm[i])) print(ans) |