from math import lcm, gcd import numpy as np class Node: def __init__(self, num, den) -> None: self.num = num self.den = den def __repr__(self) -> str: return f"{self.num}/{self.den}" def sum_lug(lugs): result = Node(0, 1) for l in lugs: r = lcm(result.den, l.den) result.num = (result.num * r//result.den + l.num * r//l.den) result.den = r return result def divide(node, value): result = gcd(node.num, value) return Node(node.num//result, node.den*(value//result)) def main(): n = int(input()) neighbours = [] lug_input = [] lug_input_sum = [] for i in range(n): neighbours.append([int(x) -1 for x in input().split()[1:]]) lug_input.append([]) lug_input_sum.append(0) lug_input[0].append(Node(1, 1)) # print(neighbours) # print(lug_input) max_den = 0 for i in range(n): lug_input_sum[i] = sum_lug(lug_input[i]) max_den = max(max_den, lug_input_sum[i].den) # print(lug_input_sum[i]) for neighbour in neighbours[i]: lug_input[neighbour].append(divide(lug_input_sum[i], len(neighbours[i]))) print(max_den) if __name__ == '__main__': main() # print(sum_lug([Node(1, 3), Node(1, 5), Node(3, 4)]))
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 | from math import lcm, gcd import numpy as np class Node: def __init__(self, num, den) -> None: self.num = num self.den = den def __repr__(self) -> str: return f"{self.num}/{self.den}" def sum_lug(lugs): result = Node(0, 1) for l in lugs: r = lcm(result.den, l.den) result.num = (result.num * r//result.den + l.num * r//l.den) result.den = r return result def divide(node, value): result = gcd(node.num, value) return Node(node.num//result, node.den*(value//result)) def main(): n = int(input()) neighbours = [] lug_input = [] lug_input_sum = [] for i in range(n): neighbours.append([int(x) -1 for x in input().split()[1:]]) lug_input.append([]) lug_input_sum.append(0) lug_input[0].append(Node(1, 1)) # print(neighbours) # print(lug_input) max_den = 0 for i in range(n): lug_input_sum[i] = sum_lug(lug_input[i]) max_den = max(max_den, lug_input_sum[i].den) # print(lug_input_sum[i]) for neighbour in neighbours[i]: lug_input[neighbour].append(divide(lug_input_sum[i], len(neighbours[i]))) print(max_den) if __name__ == '__main__': main() # print(sum_lug([Node(1, 3), Node(1, 5), Node(3, 4)])) |