import math n = int(input()) G = [] RG = [] for i in range(0, n): G.append([]) RG.append([]) for i in range(0, n): inp = list(map(int, input().split())) r = inp[0] for j in range(0, r): l = inp[j + 1] G[i].append(l - 1) RG[l - 1].append(i) vis = [False] * n Q = [] vis[0] = True Q.append(0) while Q: u = Q.pop() for v in G[u]: if not vis[v]: vis[v] = True Q.append(v) R = [] for i in range(0, n): R.append([1, 1]) R[0][0] = max(len(G[0]), 1) for i in range(1, n): if (not vis[i]) or len(G[i]) == 0: continue preReset = 1 for v in RG[i]: if vis[v]: preReset = math.lcm(preReset, R[v][0]) preDays = 0 for v in RG[i]: if vis[v]: preDays += preReset // R[v][0] * R[v][1] days = math.lcm(preDays, len(G[i])) R[i] = [(days // preDays) * preReset, days // len(G[i])] result = 1 for r in R: result = math.lcm(result, r[0]) print(result)
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 | import math n = int(input()) G = [] RG = [] for i in range(0, n): G.append([]) RG.append([]) for i in range(0, n): inp = list(map(int, input().split())) r = inp[0] for j in range(0, r): l = inp[j + 1] G[i].append(l - 1) RG[l - 1].append(i) vis = [False] * n Q = [] vis[0] = True Q.append(0) while Q: u = Q.pop() for v in G[u]: if not vis[v]: vis[v] = True Q.append(v) R = [] for i in range(0, n): R.append([1, 1]) R[0][0] = max(len(G[0]), 1) for i in range(1, n): if (not vis[i]) or len(G[i]) == 0: continue preReset = 1 for v in RG[i]: if vis[v]: preReset = math.lcm(preReset, R[v][0]) preDays = 0 for v in RG[i]: if vis[v]: preDays += preReset // R[v][0] * R[v][1] days = math.lcm(preDays, len(G[i])) R[i] = [(days // preDays) * preReset, days // len(G[i])] result = 1 for r in R: result = math.lcm(result, r[0]) print(result) |