# Zadanie: Walizki # Autor: Tomasz Kwiatkowski import math def ans(n, G, Gp): cnt = [0] * (n + 1) xd = [1] * (n + 1) res = max(1, len(G[1])) cnt[1] = res for i in range(2, n + 1): if (len(G[i]) == 0): continue c = 0 for u in Gp[i]: if (cnt[u] > 0): c += res * xd[u] // cnt[u] m = len(G[i]) res *= m // math.gcd(m, c) if (c > 0): cnt[i] = res xd[i] = c // math.gcd(m, c) return res def solve(): n = int(input()) G = [[] for i in range(n + 1)] Gp = [[] for i in range(n + 1)] for v in range(1, n + 1): G[v] = list(map(int, input().split()[1:])) for u in G[v]: Gp[u].append(v) print(ans(n, G, Gp)) solve()
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 | # Zadanie: Walizki # Autor: Tomasz Kwiatkowski import math def ans(n, G, Gp): cnt = [0] * (n + 1) xd = [1] * (n + 1) res = max(1, len(G[1])) cnt[1] = res for i in range(2, n + 1): if (len(G[i]) == 0): continue c = 0 for u in Gp[i]: if (cnt[u] > 0): c += res * xd[u] // cnt[u] m = len(G[i]) res *= m // math.gcd(m, c) if (c > 0): cnt[i] = res xd[i] = c // math.gcd(m, c) return res def solve(): n = int(input()) G = [[] for i in range(n + 1)] Gp = [[] for i in range(n + 1)] for v in range(1, n + 1): G[v] = list(map(int, input().split()[1:])) for u in G[v]: Gp[u].append(v) print(ans(n, G, Gp)) solve() |