import math n = int(input()) g = [] l = [] # print(n) # print(l) # print(g) for i in range(n): inp = input(); # print(inp) a = [int(x) for x in inp.split()] # print(a) l.append(a[0]) g.append([]) if len(a) > 1: g[i] = a[1:] else: g[i] = [] for j in range(l[i]): g[i][j] -= 1 def go(x): global g global l # print("g", g) # print("l", l) flow = [0] * n flow[0] = x for i in range(n): if l[i] == 0: continue if flow[i] % l[i] > 0: # print(flow[i], l[i]) dz = math.gcd(flow[i], l[i]) need = l[i] // dz return go(x * need) for u in g[i]: flow[u] += flow[i] // l[i] return x print(go(1)) # vi l; # vector<vi> g; # int n; # ll go(ll x) { # vector<ll> flow(n); # flow[0] = x; # rep(i, 0, n) { # if (l[i] == 0) continue; # if (flow[i] % l[i] > 0) { # // za malo dostal # ll g = gcd(flow[i], l[i]); # ll need = l[i] / g; # return go(x * need); # } # for (auto u : g[i]) { # flow[u] += flow[i] / l[i]; # } # } # return x; # } # void solve() { # cin >> n; # g.resize(n); # l.resize(n); # rep(i, 0, n) { # cin >> l[i]; # g[i].resize(l[i]); # rep(j, 0, l[i]) { # cin >> g[i][j]; # --g[i][j]; # } # } # ll ans = go(1); # cout << ans << endl; # }
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 | import math n = int(input()) g = [] l = [] # print(n) # print(l) # print(g) for i in range(n): inp = input(); # print(inp) a = [int(x) for x in inp.split()] # print(a) l.append(a[0]) g.append([]) if len(a) > 1: g[i] = a[1:] else: g[i] = [] for j in range(l[i]): g[i][j] -= 1 def go(x): global g global l # print("g", g) # print("l", l) flow = [0] * n flow[0] = x for i in range(n): if l[i] == 0: continue if flow[i] % l[i] > 0: # print(flow[i], l[i]) dz = math.gcd(flow[i], l[i]) need = l[i] // dz return go(x * need) for u in g[i]: flow[u] += flow[i] // l[i] return x print(go(1)) # vi l; # vector<vi> g; # int n; # ll go(ll x) { # vector<ll> flow(n); # flow[0] = x; # rep(i, 0, n) { # if (l[i] == 0) continue; # if (flow[i] % l[i] > 0) { # // za malo dostal # ll g = gcd(flow[i], l[i]); # ll need = l[i] / g; # return go(x * need); # } # for (auto u : g[i]) { # flow[u] += flow[i] / l[i]; # } # } # return x; # } # void solve() { # cin >> n; # g.resize(n); # l.resize(n); # rep(i, 0, n) { # cin >> l[i]; # g[i].resize(l[i]); # rep(j, 0, l[i]) { # cin >> g[i][j]; # --g[i][j]; # } # } # ll ans = go(1); # cout << ans << endl; # } |