import sys import math input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) adj = dict() n = inlt()[0] for i in range(1, n + 1): g = inlt()[1:] adj[i] = [] for x in g: adj[i].append(x) if len(adj[1]) == 0: print(1) exit(0) incoming = dict() for i in range(1, n + 1): incoming[i] = [] incoming[1].append([len(adj[1]), 1]) mia = [] for i in range(1, n + 1): if len(incoming[i]) == 0: continue licznik = 0 mianownik = 1 for [L, M] in incoming[i]: tmpa = licznik * M + mianownik * L tmpb = mianownik * M licznik = tmpa mianownik = tmpb if len(adj[i]) == 0: continue mianownik *= len(adj[i]) gg = math.gcd(licznik, mianownik) licznik = licznik // gg mianownik = mianownik // gg for to in adj[i]: incoming[to].append([licznik, mianownik]) mia.append(mianownik) lcm = 1 for x in mia: lcm = (x * lcm) // math.gcd(lcm, x) print(lcm * len(adj[1]))
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 | import sys import math input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) adj = dict() n = inlt()[0] for i in range(1, n + 1): g = inlt()[1:] adj[i] = [] for x in g: adj[i].append(x) if len(adj[1]) == 0: print(1) exit(0) incoming = dict() for i in range(1, n + 1): incoming[i] = [] incoming[1].append([len(adj[1]), 1]) mia = [] for i in range(1, n + 1): if len(incoming[i]) == 0: continue licznik = 0 mianownik = 1 for [L, M] in incoming[i]: tmpa = licznik * M + mianownik * L tmpb = mianownik * M licznik = tmpa mianownik = tmpb if len(adj[i]) == 0: continue mianownik *= len(adj[i]) gg = math.gcd(licznik, mianownik) licznik = licznik // gg mianownik = mianownik // gg for to in adj[i]: incoming[to].append([licznik, mianownik]) mia.append(mianownik) lcm = 1 for x in mia: lcm = (x * lcm) // math.gcd(lcm, x) print(lcm * len(adj[1])) |