def nwd(a, b): if a == 0: return b if b == 0: return a if a < b: return nwd(b % a, a) return nwd(a % b, b) def fillluggage(l1, luggage, size, graph): luggage[1] = l1 for i in range(2, len(luggage)): luggage[i] = 0 for i in range(1, len(graph)): for j in range(size[i]): luggage[graph[i][j]] += int((luggage[i] + size[i] - 1 - j)/size[i]) return luggage n = int(input()) graph = [[]] size = [0] luggage = [0] for i in range(1, n + 1): inputLine = [int(x) for x in input().split()] graph.append([]) size.append(inputLine[0]) luggage.append(0) for j in range(1, inputLine[0] + 1): graph[i].append(inputLine[j]) if size[1] == 0: print(1) else: minCount = size[1] luggage = fillluggage(minCount, luggage, size, graph) for i in range(1, n+1): if size[i] == 0: continue if luggage[i] % size[i] == 0: continue minCount *= (size[i]/nwd(size[i], luggage[i])) fillluggage(minCount, luggage, size, graph) print(int(minCount))
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 | def nwd(a, b): if a == 0: return b if b == 0: return a if a < b: return nwd(b % a, a) return nwd(a % b, b) def fillluggage(l1, luggage, size, graph): luggage[1] = l1 for i in range(2, len(luggage)): luggage[i] = 0 for i in range(1, len(graph)): for j in range(size[i]): luggage[graph[i][j]] += int((luggage[i] + size[i] - 1 - j)/size[i]) return luggage n = int(input()) graph = [[]] size = [0] luggage = [0] for i in range(1, n + 1): inputLine = [int(x) for x in input().split()] graph.append([]) size.append(inputLine[0]) luggage.append(0) for j in range(1, inputLine[0] + 1): graph[i].append(inputLine[j]) if size[1] == 0: print(1) else: minCount = size[1] luggage = fillluggage(minCount, luggage, size, graph) for i in range(1, n+1): if size[i] == 0: continue if luggage[i] % size[i] == 0: continue minCount *= (size[i]/nwd(size[i], luggage[i])) fillluggage(minCount, luggage, size, graph) print(int(minCount)) |