def gcd(a, b): a = int(a) b = int(b) while b != 0: a, b = int(b), int(a%b) return a src_cnt = [0 for i in range(105)] reset = [list((0,0)) for i in range(105)] v = [[] for i in range(105)] src = [[] for i in range(105)] n = int(input()) for i in range(1,n+1): conn = list(map(int, input().strip().split())) conn.pop(0) for a in conn: v[i].append(a) src[a].append(i) src_cnt[a]+=1 for i in range(2,n+1): if src_cnt[i] == 0: for a in v[i]: src_cnt[a]-=1 reset[1] = [len(v[1]), 1] for i in range(2,n+1): if src_cnt[i] > 0: reset[i] = [1,0] for a in v[1]: src_cnt[a]-=1 mmax = int(max(1, reset[1][0])) for i in range(2,n+1): if src_cnt[i] == 0 and reset[i][0] > 0: nww = 1 for a in src[i]: if reset[a][0] > 0: nww = nww // gcd(nww, reset[a][0]) * reset[a][0] if len(v[i]) == 0: mmax = max(mmax, nww) continue sum = 0 for a in src[i]: if reset[a][0] > 0: sum += nww // reset[a][0] * reset[a][1] w = sum // gcd(sum, len(v[i])) * len(v[i]) reset[i][1] = w // len(v[i]) reset[i][0] = w // sum * nww mmax = max(mmax, reset[i][0]) for a in v[i]: src_cnt[a]-=1 print(int(mmax))
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 | def gcd(a, b): a = int(a) b = int(b) while b != 0: a, b = int(b), int(a%b) return a src_cnt = [0 for i in range(105)] reset = [list((0,0)) for i in range(105)] v = [[] for i in range(105)] src = [[] for i in range(105)] n = int(input()) for i in range(1,n+1): conn = list(map(int, input().strip().split())) conn.pop(0) for a in conn: v[i].append(a) src[a].append(i) src_cnt[a]+=1 for i in range(2,n+1): if src_cnt[i] == 0: for a in v[i]: src_cnt[a]-=1 reset[1] = [len(v[1]), 1] for i in range(2,n+1): if src_cnt[i] > 0: reset[i] = [1,0] for a in v[1]: src_cnt[a]-=1 mmax = int(max(1, reset[1][0])) for i in range(2,n+1): if src_cnt[i] == 0 and reset[i][0] > 0: nww = 1 for a in src[i]: if reset[a][0] > 0: nww = nww // gcd(nww, reset[a][0]) * reset[a][0] if len(v[i]) == 0: mmax = max(mmax, nww) continue sum = 0 for a in src[i]: if reset[a][0] > 0: sum += nww // reset[a][0] * reset[a][1] w = sum // gcd(sum, len(v[i])) * len(v[i]) reset[i][1] = w // len(v[i]) reset[i][0] = w // sum * nww mmax = max(mmax, reset[i][0]) for a in v[i]: src_cnt[a]-=1 print(int(mmax)) |