import math n = int(input()) siz = [0] * (n + 1) prev = [[] for _ in range(n + 1)] den = 1 for i in range(1, n + 1): v = list(map(int, input().split()))[1:] siz[i] = len(v) den *= max(1, siz[i]) for j in v: prev[j].append(i) dp = [0] * (n + 1) dp[1] = den gcd = den for i in range(2, n + 1): for j in prev[i]: dp[i] += dp[j] // siz[j] gcd = math.gcd(gcd, dp[i]) print(den // gcd)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import math n = int(input()) siz = [0] * (n + 1) prev = [[] for _ in range(n + 1)] den = 1 for i in range(1, n + 1): v = list(map(int, input().split()))[1:] siz[i] = len(v) den *= max(1, siz[i]) for j in v: prev[j].append(i) dp = [0] * (n + 1) dp[1] = den gcd = den for i in range(2, n + 1): for j in prev[i]: dp[i] += dp[j] // siz[j] gcd = math.gcd(gcd, dp[i]) print(den // gcd) |