def gcd( a, b): while(a != 0 and b != 0): if (a > b): a = int(a % b) else: b = int(b % a) return int(a + b) class Rational: def __init__(self, l, m): self.l = int(l) self.m = int(m) def __add__(self, r): d = gcd(self.m, r.m) mTmp = int(self.m * int(r.m // d)) lTmp = int(self.l * int(r.m // d) + r.l * int(self.m // d)) d2 = gcd(lTmp, mTmp) return Rational(int(lTmp // d2), int(mTmp // d2)) def divide(self, n): tmpM = int(self.m * n) tmpL = int(self.l) d = gcd(tmpL, tmpM) return Rational(tmpL // d, tmpM // d) def __str__(self): return "({0}/{1})".format(self.l, self.m) def __repr__(self): return "({0}/{1})".format(self.l, self.m) def createGraph(n): ret = [] for _i in range(n): ret.append([]); return ret visited = [0] * 200 def visitAllNodes(n, graph): if (visited[n] == 1): return visited[n] = 1; for v in graph[n]: #(int i = 0; i < (int)graph[n].size(); ++i) visitAllNodes(v, graph); def calculateValues( n, revGraph, tmpValues, multipier): ret = Rational(0,1) if (tmpValues[n].l > 0): return tmpValues[n] for v in revGraph[n]: #(int i = 0; i < (int)revGraph[n].size(); ++i){ if (visited[v] == 1): ret = ret + calculateValues(v, revGraph, tmpValues, multipier).divide(multipier[v]) tmpValues[n] = ret; return ret; if __name__ == '__main__': # lines = [l for l in sys.stdin] n = int(input()) graph = createGraph(n + 1) revGraph = createGraph(n + 1) leaves = [] multipier = [0] * 200 for v1 in range(1, n + 1): k = input().split() if (int(k[0]) == 0): leaves.append(v1) multipier[v1] = int(k[0]) for j in range(1, int(k[0]) + 1): graph[v1].append(int(k[j])) revGraph[int(k[j])].append(v1) # print(graph) # print(revGraph) visitAllNodes(1,graph) tmpValues = [] for i in range(200): tmpValues.append(Rational(0,1)) tmpValues[1] = Rational(1, 1) for v in leaves: if (visited[v] == 1): calculateValues(v, revGraph, tmpValues, multipier); # print(leaves) # print(multipier) # print(tmpValues) # for i in range(1, n + 1): # print(i, tmpValues[i]) toAgree = []; for v in range(1, n + 1): if (multipier[v] > 1 and visited[v] == 1): toAgree.append(tmpValues[v].divide(multipier[v])) # print(toAgree) result = 1 for r in toAgree: d = gcd(result, r.m) # print (result, r.m, int(r.m / d), d, int(result/d)) result = result // d * r.m 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | def gcd( a, b): while(a != 0 and b != 0): if (a > b): a = int(a % b) else: b = int(b % a) return int(a + b) class Rational: def __init__(self, l, m): self.l = int(l) self.m = int(m) def __add__(self, r): d = gcd(self.m, r.m) mTmp = int(self.m * int(r.m // d)) lTmp = int(self.l * int(r.m // d) + r.l * int(self.m // d)) d2 = gcd(lTmp, mTmp) return Rational(int(lTmp // d2), int(mTmp // d2)) def divide(self, n): tmpM = int(self.m * n) tmpL = int(self.l) d = gcd(tmpL, tmpM) return Rational(tmpL // d, tmpM // d) def __str__(self): return "({0}/{1})".format(self.l, self.m) def __repr__(self): return "({0}/{1})".format(self.l, self.m) def createGraph(n): ret = [] for _i in range(n): ret.append([]); return ret visited = [0] * 200 def visitAllNodes(n, graph): if (visited[n] == 1): return visited[n] = 1; for v in graph[n]: #(int i = 0; i < (int)graph[n].size(); ++i) visitAllNodes(v, graph); def calculateValues( n, revGraph, tmpValues, multipier): ret = Rational(0,1) if (tmpValues[n].l > 0): return tmpValues[n] for v in revGraph[n]: #(int i = 0; i < (int)revGraph[n].size(); ++i){ if (visited[v] == 1): ret = ret + calculateValues(v, revGraph, tmpValues, multipier).divide(multipier[v]) tmpValues[n] = ret; return ret; if __name__ == '__main__': # lines = [l for l in sys.stdin] n = int(input()) graph = createGraph(n + 1) revGraph = createGraph(n + 1) leaves = [] multipier = [0] * 200 for v1 in range(1, n + 1): k = input().split() if (int(k[0]) == 0): leaves.append(v1) multipier[v1] = int(k[0]) for j in range(1, int(k[0]) + 1): graph[v1].append(int(k[j])) revGraph[int(k[j])].append(v1) # print(graph) # print(revGraph) visitAllNodes(1,graph) tmpValues = [] for i in range(200): tmpValues.append(Rational(0,1)) tmpValues[1] = Rational(1, 1) for v in leaves: if (visited[v] == 1): calculateValues(v, revGraph, tmpValues, multipier); # print(leaves) # print(multipier) # print(tmpValues) # for i in range(1, n + 1): # print(i, tmpValues[i]) toAgree = []; for v in range(1, n + 1): if (multipier[v] > 1 and visited[v] == 1): toAgree.append(tmpValues[v].divide(multipier[v])) # print(toAgree) result = 1 for r in toAgree: d = gcd(result, r.m) # print (result, r.m, int(r.m / d), d, int(result/d)) result = result // d * r.m print(result) |