def gcd(x, y): while y != 0: x, y = y, x % y return x def nww(x,y): return (x*y)//gcd(x,y) n = int(input()) ans = int(1) L = [int(0)] * (n+2) M = [int(1)] * (n+2); L[1] = int(1) for i in range(1,n+1): line = input() line = line.split() k = int(line[0]) if k == 0: continue if L[i] == 0: continue p = nww(L[i],k) p = p//L[i] L[i] *= p L[i] //= k M[i] *= p if M[i] > ans: ans = M[i] for j in range(1,k+1): x = int(line[j]) p = nww(M[i],M[x]) p2 = p//M[i] L[x] *= p//M[x] L[x] += L[i] * p2 M[x] = p 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 38 39 40 41 42 43 44 45 46 | def gcd(x, y): while y != 0: x, y = y, x % y return x def nww(x,y): return (x*y)//gcd(x,y) n = int(input()) ans = int(1) L = [int(0)] * (n+2) M = [int(1)] * (n+2); L[1] = int(1) for i in range(1,n+1): line = input() line = line.split() k = int(line[0]) if k == 0: continue if L[i] == 0: continue p = nww(L[i],k) p = p//L[i] L[i] *= p L[i] //= k M[i] *= p if M[i] > ans: ans = M[i] for j in range(1,k+1): x = int(line[j]) p = nww(M[i],M[x]) p2 = p//M[i] L[x] *= p//M[x] L[x] += L[i] * p2 M[x] = p print(ans) |