n = int(input()) G = {} for i in range(1, n + 1): G[i] = [] data = input().split() for l in data[1 : ]: G[i].append(int(l)) def gcd(a, b): if b == 0: return a return gcd(b, a % b) def add(p1, p2): r = (p1[0] * p2[1] + p1[1] * p2[0], p1[1] * p2[1]) if r[0] == 0: return (0, 1) d = gcd(r[0], r[1]) return (r[0] // d, r[1] // d) def mul(p1, p2): return (p1[0] * p2[0], p1[1] * p2[1]) DP = { i : (0, 1) for i in range(1, n + 1) } DP[1] = (1, 1) for i in range(1, n + 1): for x in G[i]: DP[x] = add(DP[x], mul(DP[i], (1, len(G[i])))) out = 1 for i in range(1, n + 1): out = out * DP[i][1] // gcd(out, DP[i][1]) print(out)
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 | n = int(input()) G = {} for i in range(1, n + 1): G[i] = [] data = input().split() for l in data[1 : ]: G[i].append(int(l)) def gcd(a, b): if b == 0: return a return gcd(b, a % b) def add(p1, p2): r = (p1[0] * p2[1] + p1[1] * p2[0], p1[1] * p2[1]) if r[0] == 0: return (0, 1) d = gcd(r[0], r[1]) return (r[0] // d, r[1] // d) def mul(p1, p2): return (p1[0] * p2[0], p1[1] * p2[1]) DP = { i : (0, 1) for i in range(1, n + 1) } DP[1] = (1, 1) for i in range(1, n + 1): for x in G[i]: DP[x] = add(DP[x], mul(DP[i], (1, len(G[i])))) out = 1 for i in range(1, n + 1): out = out * DP[i][1] // gcd(out, DP[i][1]) print(out) |