from math import gcd, lcm n = int(input()) adj = [[]] + [[int(x) for x in input().split()[1:]] for _ in range(n)] en = [0] * (n + 1) dn = [1] * (n + 1) en[1] = 1 res = 1 for i in range(1, n + 1): if not len(adj[i]) or not en[i]: continue d = gcd(len(adj[i]), en[i]) en[i] //= d dn[i] *= len(adj[i]) // d res = lcm(res, dn[i]) for v in adj[i]: en[v], dn[v] = en[v] * dn[i] + en[i] * dn[v], dn[i] * dn[v] d = gcd(en[v], dn[v]) en[v] //= d dn[v] //= d print(res)
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 | from math import gcd, lcm n = int(input()) adj = [[]] + [[int(x) for x in input().split()[1:]] for _ in range(n)] en = [0] * (n + 1) dn = [1] * (n + 1) en[1] = 1 res = 1 for i in range(1, n + 1): if not len(adj[i]) or not en[i]: continue d = gcd(len(adj[i]), en[i]) en[i] //= d dn[i] *= len(adj[i]) // d res = lcm(res, dn[i]) for v in adj[i]: en[v], dn[v] = en[v] * dn[i] + en[i] * dn[v], dn[i] * dn[v] d = gcd(en[v], dn[v]) en[v] //= d dn[v] //= d print(res) |