# -*- coding: utf-8 -*- """ Created on Sun Dec 18 12:54:28 2022 @author: tkaz """ def nwd(a,b): if b==0: return a return nwd(b, a%b) def nww(a,b): return a*b//nwd(a,b) class Platform: def __init__(self): self.nexts = [] self.period = 1 self.filled = 0 ps = [] n = int(input()) for _ in range(n): ps.append( Platform() ) ps[-1].nexts = list(map(lambda x: int(x)-1, input().split()[1:])) res = 1 ps[0].filled = 1 cnt = 0 for p in ps: cnt+=1 #print(f"{cnt}\t{res}\t{p.period}\t{p.filled}") if (not p.nexts) or p.filled==0: continue p.period *= len(p.nexts) divider = nwd(p.filled, p.period) p.period //= divider p.filled //= divider new_period = p.period #* len(p.nexts) res = nww(res, new_period) for next in p.nexts: period2 = nww(new_period, ps[next].period) ps[next].filled *= period2 // ps[next].period ps[next].filled += period2 * p.filled // new_period ps[next].period = period2 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | # -*- coding: utf-8 -*- """ Created on Sun Dec 18 12:54:28 2022 @author: tkaz """ def nwd(a,b): if b==0: return a return nwd(b, a%b) def nww(a,b): return a*b//nwd(a,b) class Platform: def __init__(self): self.nexts = [] self.period = 1 self.filled = 0 ps = [] n = int(input()) for _ in range(n): ps.append( Platform() ) ps[-1].nexts = list(map(lambda x: int(x)-1, input().split()[1:])) res = 1 ps[0].filled = 1 cnt = 0 for p in ps: cnt+=1 #print(f"{cnt}\t{res}\t{p.period}\t{p.filled}") if (not p.nexts) or p.filled==0: continue p.period *= len(p.nexts) divider = nwd(p.filled, p.period) p.period //= divider p.filled //= divider new_period = p.period #* len(p.nexts) res = nww(res, new_period) for next in p.nexts: period2 = nww(new_period, ps[next].period) ps[next].filled *= period2 // ps[next].period ps[next].filled += period2 * p.filled // new_period ps[next].period = period2 print(res) |