from queue import Queue graf = [[] for i in range(109)] indeg = [0 for i in range(109)] litab = [0 for i in range(109)] mitab = [1 for i in range(109)] litab[1] = 1 q = Queue() wyn = 1 def NWD(a, b): if b == 0: return a if a < b: a,b = b,a return NWD(b, a % b) def NWW(a, b): c = NWD(a, b) return (a * b) // c n = int(input()) for i in range(1, n+1): line = [int(i) for i in input().split(' ')] graf[i] = line[1:] for j in graf[i]: indeg[j] += 1 for i in range(2, n+1): if indeg[i] == 0: q.put(i) while not q.empty(): a = q.get() for i in graf[a]: indeg[i] -= 1 if indeg[i] == 0: q.put(i) q.put(1); while not q.empty(): u = q.get() li = litab[u] mi = mitab[u] d = len(graf[u]) if d == 0: continue a = NWD(li, d) d //= a li //= a mi *= d wyn = NWW(mi, wyn) for b in graf[u]: lib = litab[b] mib = mitab[b] d = NWW(mi, mib) lib = (d // mib) * lib a = (d // mi) * li lib += a litab[b] = lib mitab[b] = d indeg[b] -= 1 if indeg[b] == 0: q.put(b) print(wyn)
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 | from queue import Queue graf = [[] for i in range(109)] indeg = [0 for i in range(109)] litab = [0 for i in range(109)] mitab = [1 for i in range(109)] litab[1] = 1 q = Queue() wyn = 1 def NWD(a, b): if b == 0: return a if a < b: a,b = b,a return NWD(b, a % b) def NWW(a, b): c = NWD(a, b) return (a * b) // c n = int(input()) for i in range(1, n+1): line = [int(i) for i in input().split(' ')] graf[i] = line[1:] for j in graf[i]: indeg[j] += 1 for i in range(2, n+1): if indeg[i] == 0: q.put(i) while not q.empty(): a = q.get() for i in graf[a]: indeg[i] -= 1 if indeg[i] == 0: q.put(i) q.put(1); while not q.empty(): u = q.get() li = litab[u] mi = mitab[u] d = len(graf[u]) if d == 0: continue a = NWD(li, d) d //= a li //= a mi *= d wyn = NWW(mi, wyn) for b in graf[u]: lib = litab[b] mib = mitab[b] d = NWW(mi, mib) lib = (d // mib) * lib a = (d // mi) * li lib += a litab[b] = lib mitab[b] = d indeg[b] -= 1 if indeg[b] == 0: q.put(b) print(wyn) |