def gcdM(a, b): if (a == 0): return b return gcdM(b % a, a) def calcNum(num, g, m, beg=0): for i in range(beg, m): for v in g[i]: num[v] += num[i] / len(g[i]) def fix(num, g, i): mult = len(g[i]) / gcdM(num[i], len(g[i])) # print(i, mult) if mult == 1: return calcNum(num, g, i + 1, i) num[0] *= mult num[1:] = [0] * (len(num) - 1) calcNum(num, g, i + 1) def solve(g): if (len(g[0]) == 0): return 1 num = [0] * len(g) num[0] = len(g[0]) calcNum(num, g, 1) for i in range(1, len(g)): if (len(g[i]) == 0 or num[i] == 0): continue fix(num, g, i) return num[0] n = int(input()) g = [[] for i in range(n)] for v in range(n): l = [int(x) for x in input().split()] l = l[1:] for u in l: u -= 1 g[v].append(u) print(int(solve(g)))
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 | def gcdM(a, b): if (a == 0): return b return gcdM(b % a, a) def calcNum(num, g, m, beg=0): for i in range(beg, m): for v in g[i]: num[v] += num[i] / len(g[i]) def fix(num, g, i): mult = len(g[i]) / gcdM(num[i], len(g[i])) # print(i, mult) if mult == 1: return calcNum(num, g, i + 1, i) num[0] *= mult num[1:] = [0] * (len(num) - 1) calcNum(num, g, i + 1) def solve(g): if (len(g[0]) == 0): return 1 num = [0] * len(g) num[0] = len(g[0]) calcNum(num, g, 1) for i in range(1, len(g)): if (len(g[i]) == 0 or num[i] == 0): continue fix(num, g, i) return num[0] n = int(input()) g = [[] for i in range(n)] for v in range(n): l = [int(x) for x in input().split()] l = l[1:] for u in l: u -= 1 g[v].append(u) print(int(solve(g))) |