import sys edges = [[] for i in range(111)] f = [[] for i in range(111)] visited = [False for i in range(111)] result = 1 def nwd(a, b): while (b): r = a % b a = b b = r return a def nww(a, b): return b*(a//nwd(a, b)) n = int(sys.stdin.readline()) for i in range(n): line = sys.stdin.readline().split(" "); r=int(line[0]) for ri in range(r): edges[i+1].append(int(line[ri+1])) visited[1] = True f[1].append([1, 1]) for i in range(n): if ((not visited[i+1]) or len(edges[i+1]) == 0): continue ulamek=[0, 1] for fi in f[i+1]: ulamek[1] = nww(fi[1], ulamek[1]) for fi in f[i+1]: ulamek[0] += fi[0] * ulamek[1] // fi[1] if (len(edges[i+1]) >= 2): d = nwd(len(edges[i+1]) * ulamek[1], ulamek[0]) result = nww(result, len(edges[i+1]) * ulamek[1]//d) ulamek[1] = ulamek[1] * len(edges[i+1]) for ei in edges[i+1]: f[ei].append(ulamek) visited[ei] = True print(result)
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 | import sys edges = [[] for i in range(111)] f = [[] for i in range(111)] visited = [False for i in range(111)] result = 1 def nwd(a, b): while (b): r = a % b a = b b = r return a def nww(a, b): return b*(a//nwd(a, b)) n = int(sys.stdin.readline()) for i in range(n): line = sys.stdin.readline().split(" "); r=int(line[0]) for ri in range(r): edges[i+1].append(int(line[ri+1])) visited[1] = True f[1].append([1, 1]) for i in range(n): if ((not visited[i+1]) or len(edges[i+1]) == 0): continue ulamek=[0, 1] for fi in f[i+1]: ulamek[1] = nww(fi[1], ulamek[1]) for fi in f[i+1]: ulamek[0] += fi[0] * ulamek[1] // fi[1] if (len(edges[i+1]) >= 2): d = nwd(len(edges[i+1]) * ulamek[1], ulamek[0]) result = nww(result, len(edges[i+1]) * ulamek[1]//d) ulamek[1] = ulamek[1] * len(edges[i+1]) for ei in edges[i+1]: f[ei].append(ulamek) visited[ei] = True print(result) |