import math n = int(input()) ado = [[] for _ in range (0, n)] for i in range(0, n): ado[i] = [int(x)-1 for x in input().split()][1:] if len(ado[0]) == 0: print(1) quit() vis_per = [0 for _ in range(0, n)] vis_cnt = [0 for _ in range(0, n)] out_per = [0 for _ in range(0, n)] out_cnt = [0 for _ in range(0, n)] adi = [[] for _ in range (0, n)] for i in range(0, n): if i > 0 and len(adi[i]) == 0: continue for j in ado[i]: adi[j].append(i) out_per[0] = len(ado[0]) out_cnt[0] = 1 ans = out_per[0] for i in range(1, n): if len(adi[i]) == 0 or len(ado[i]) == 0: continue vis_per[i] = 1; for j in adi[i]: vis_per[i] = math.lcm(vis_per[i], out_per[j]) for j in adi[i]: vis_cnt[i] = vis_cnt[i]+vis_per[i]//out_per[j]*out_cnt[j] outdeg = len(ado[i]) d = math.gcd(vis_cnt[i], outdeg) out_per[i] = outdeg//d*vis_per[i] out_cnt[i] = vis_cnt[i]//d ans = math.lcm(ans, out_per[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 38 39 40 41 42 43 | import math n = int(input()) ado = [[] for _ in range (0, n)] for i in range(0, n): ado[i] = [int(x)-1 for x in input().split()][1:] if len(ado[0]) == 0: print(1) quit() vis_per = [0 for _ in range(0, n)] vis_cnt = [0 for _ in range(0, n)] out_per = [0 for _ in range(0, n)] out_cnt = [0 for _ in range(0, n)] adi = [[] for _ in range (0, n)] for i in range(0, n): if i > 0 and len(adi[i]) == 0: continue for j in ado[i]: adi[j].append(i) out_per[0] = len(ado[0]) out_cnt[0] = 1 ans = out_per[0] for i in range(1, n): if len(adi[i]) == 0 or len(ado[i]) == 0: continue vis_per[i] = 1; for j in adi[i]: vis_per[i] = math.lcm(vis_per[i], out_per[j]) for j in adi[i]: vis_cnt[i] = vis_cnt[i]+vis_per[i]//out_per[j]*out_cnt[j] outdeg = len(ado[i]) d = math.gcd(vis_cnt[i], outdeg) out_per[i] = outdeg//d*vis_per[i] out_cnt[i] = vis_cnt[i]//d ans = math.lcm(ans, out_per[i]) print(ans) |