import math n = int(input()) g = [[]] *(n+1) deg = [0]*(n+1) for i in range(1, n+1): arr = list(map(int, input().split())) deg[i] = arr[0] tab = [] for j in range(1, deg[i]+1): tab.append(arr[j]) g[i] = tab total = [0]*(n+1) res = 1 total[1] = 1 for i in range(1, n+1): if(total[i] != 0 and deg[i] != 0): d = math.gcd(total[i], deg[i]) mul = deg[i] // d for j in range(i, n+1): total[j] *= mul res *= mul for x in g[i]: total[x] += total[i]//deg[i] #print(total) print(res)
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 | import math n = int(input()) g = [[]] *(n+1) deg = [0]*(n+1) for i in range(1, n+1): arr = list(map(int, input().split())) deg[i] = arr[0] tab = [] for j in range(1, deg[i]+1): tab.append(arr[j]) g[i] = tab total = [0]*(n+1) res = 1 total[1] = 1 for i in range(1, n+1): if(total[i] != 0 and deg[i] != 0): d = math.gcd(total[i], deg[i]) mul = deg[i] // d for j in range(i, n+1): total[j] *= mul res *= mul for x in g[i]: total[x] += total[i]//deg[i] #print(total) print(res) |