import sys input = sys.stdin.readline def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def nwd(a, b): return a if b == 0 else nwd(b, a % b) def nww(a, b): return a * b // nwd(a, b) graf = [] n = inp() graf.append([]) tab = [0] for i in range(1, n + 1): tab.append(0) ll = inlt() a = ll[0] al = [] for j in range(1, len(ll)): al.append(ll[j]) graf.append(al) tab[1] = 1 res = 1 for i in range(1, n + 1): if len(graf[i]) > 0 and tab[i] > 0: if tab[i] % len(graf[i]) != 0: l = nww(tab[i], len(graf[i])) mn = l // tab[i] for j in range(i, n + 1): tab[j] = tab[j] * mn res = res * mn for j in range(len(graf[i])): tab[graf[i][j]] = tab[graf[i][j]] + (tab[i] // len(graf[i])) sys.stdout.write(str(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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import sys input = sys.stdin.readline def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def nwd(a, b): return a if b == 0 else nwd(b, a % b) def nww(a, b): return a * b // nwd(a, b) graf = [] n = inp() graf.append([]) tab = [0] for i in range(1, n + 1): tab.append(0) ll = inlt() a = ll[0] al = [] for j in range(1, len(ll)): al.append(ll[j]) graf.append(al) tab[1] = 1 res = 1 for i in range(1, n + 1): if len(graf[i]) > 0 and tab[i] > 0: if tab[i] % len(graf[i]) != 0: l = nww(tab[i], len(graf[i])) mn = l // tab[i] for j in range(i, n + 1): tab[j] = tab[j] * mn res = res * mn for j in range(len(graf[i])): tab[graf[i][j]] = tab[graf[i][j]] + (tab[i] // len(graf[i])) sys.stdout.write(str(res)) |