def read(): def get_numbers(): try: st=input() while st=='':st=input() read.s = map(eval,st.split()) return 1 except: return 0 if not hasattr(read, 's'): if not get_numbers():return 0 try: return next(read.s) except StopIteration: if not get_numbers(): return 0 else: return next(read.s) def gcd(a, b): if b > 0: return gcd(b, a % b) else : return a def lcm(a, b): return a // gcd(a, b) * b n = read() G = [] for i in range(n) : k = read() deg = [] for j in range(k) : deg.append(read() - 1) G.append(deg) f = [] g = [] for i in range(n) : f.append(0) g.append(1) f[0] = g[0] = 1 for u in range(n) : if len(G[u]): x = g[u] * len(G[u]) for v in G[u] : f[v] = f[v] * x + f[u] * g[v] g[v] = g[v] * x t = gcd(f[v], g[v]) f[v] = f[v] // t g[v] = g[v] // t ans = 1 for i in range(n) : ans = lcm(ans, g[i]) print(ans)
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 | def read(): def get_numbers(): try: st=input() while st=='':st=input() read.s = map(eval,st.split()) return 1 except: return 0 if not hasattr(read, 's'): if not get_numbers():return 0 try: return next(read.s) except StopIteration: if not get_numbers(): return 0 else: return next(read.s) def gcd(a, b): if b > 0: return gcd(b, a % b) else : return a def lcm(a, b): return a // gcd(a, b) * b n = read() G = [] for i in range(n) : k = read() deg = [] for j in range(k) : deg.append(read() - 1) G.append(deg) f = [] g = [] for i in range(n) : f.append(0) g.append(1) f[0] = g[0] = 1 for u in range(n) : if len(G[u]): x = g[u] * len(G[u]) for v in G[u] : f[v] = f[v] * x + f[u] * g[v] g[v] = g[v] * x t = gcd(f[v], g[v]) f[v] = f[v] // t g[v] = g[v] // t ans = 1 for i in range(n) : ans = lcm(ans, g[i]) print(ans) |