#!/usr/bin/env python3
def gcd(A, B):
a = A
b = B
if (a < b):
x = b
y = a
a = x
b = y
r = 1
while (r > 0):
r = a % b
a = b
b = r
return a
def lcm(a, b):
return (a * b) // gcd(a, b);
def main():
n = int(input())
nplusone = n + 1;
out = []
ing = []
for i in range(nplusone):
out.append([])
ing.append([])
for i in range(1, nplusone):
lst = input().split()
r = int(lst[0])
for j in range(1, r + 1):
l = int(lst[j])
out[i].append(l)
ing[l].append(i)
connected = []
for i in range(nplusone):
connected.append(False)
connected[1] = True
for i in range(2, nplusone):
p = False
for j in range(len(ing[i])):
if (connected[ing[i][j]]):
p = True
connected[i] = p
dp = []
for i in range(nplusone):
dp.append(1)
for i in range(2, nplusone):
if (len(out[i]) > 0 and connected[i]):
L = 0
for j in range(len(ing[i])):
if (connected[ing[i][j]]):
L += dp[ing[i][j]]
s = len(out[i])
LCM = lcm(s, L)
dp[i] = LCM // s
for j in range(1, i):
dp[j] *= LCM // L
if (len(out[1]) > 0):
res = dp[1] * len(out[1])
else:
res = 1
print(res)
return 0;
main()
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 | #!/usr/bin/env python3 def gcd(A, B): a = A b = B if (a < b): x = b y = a a = x b = y r = 1 while (r > 0): r = a % b a = b b = r return a def lcm(a, b): return (a * b) // gcd(a, b); def main(): n = int(input()) nplusone = n + 1; out = [] ing = [] for i in range(nplusone): out.append([]) ing.append([]) for i in range(1, nplusone): lst = input().split() r = int(lst[0]) for j in range(1, r + 1): l = int(lst[j]) out[i].append(l) ing[l].append(i) connected = [] for i in range(nplusone): connected.append(False) connected[1] = True for i in range(2, nplusone): p = False for j in range(len(ing[i])): if (connected[ing[i][j]]): p = True connected[i] = p dp = [] for i in range(nplusone): dp.append(1) for i in range(2, nplusone): if (len(out[i]) > 0 and connected[i]): L = 0 for j in range(len(ing[i])): if (connected[ing[i][j]]): L += dp[ing[i][j]] s = len(out[i]) LCM = lcm(s, L) dp[i] = LCM // s for j in range(1, i): dp[j] *= LCM // L if (len(out[1]) > 0): res = dp[1] * len(out[1]) else: res = 1 print(res) return 0; main() |
English