|
Download tasks/equivalent-program-pair-search/contexts/sample-00016.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 260 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00016.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00016.md
-
curl -L -o sample-00016.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00016.md
260 kB
| ### 001 | |
| ```python | |
| import math as m | |
| n = int(input()) | |
| cost = list(map(int, input().split())) | |
| s = [] | |
| sr = [] | |
| for i in range(n): | |
| s.append(list(input())) | |
| sr.append(list(reversed(s[i]))) | |
| dp = [[0, 0] for i in range(n + 1)] | |
| flag = 0 | |
| dp[0][1] = cost[0] | |
| for i in range(1, n): | |
| if dp[i - 1][0] == -1 and dp[i - 1][1] == -1: | |
| flag = 1 | |
| break | |
| elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1: | |
| if s[i] >= s[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1: | |
| if s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| else: | |
| if s[i] >= s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) | |
| elif s[i] >= s[i - 1] and s[i] < sr[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| elif s[i] < s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i] | |
| elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| if flag == 1: | |
| print(-1) | |
| else: | |
| res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1]) | |
| print(res) | |
| ``` | |
| ### 002 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 0 and vis[i - 1][j] == 4 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 003 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S - tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 0: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h < 0: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 004 | |
| ```python | |
| n, c, d = map(int, input().split()) | |
| inf = 10 ** 9 | |
| a = [0] * n | |
| sc = [] | |
| sd = [] | |
| for i in range(n): | |
| b, p, t = input().split() | |
| b = int(b) | |
| p = int(p) | |
| a[i] = [b, p, t] | |
| if t == 'C': | |
| sc.append([p, b]) | |
| else: | |
| sd.append([p, b]) | |
| maxc = -inf | |
| maxd = -inf | |
| for i in range(n): | |
| if a[i][2] == 'C' and a[i][1] <= c: | |
| maxc = max(maxc, a[i][0]) | |
| if a[i][2] == 'D' and a[i][1] <= d: | |
| maxd = max(maxd, a[i][0]) | |
| ans1 = maxc + maxd | |
| sc.sort() | |
| scpref = [0] * (len(sc) + 1) | |
| scpref[0] = -inf | |
| for i in range(len(sc)): | |
| scpref[i + 1] = max(scpref[i], sc[i][1]) | |
| ans2 = -inf | |
| for i in range(len(sc)): | |
| l = -1 | |
| r = i | |
| while l < r - 1: | |
| m = (l + r) // 2 | |
| if not sc[m][0] <= c - sc[i][0]: | |
| l = m | |
| else: | |
| r = m | |
| ans2 = max(ans2, sc[i][1] + scpref[l + 1]) | |
| sd.sort() | |
| sdpref = [0] * (len(sd) + 1) | |
| sdpref[0] = -inf | |
| for i in range(len(sd)): | |
| sdpref[i + 1] = max(sdpref[i], sd[i][1]) | |
| ans3 = -inf | |
| for i in range(len(sd)): | |
| l = -1 | |
| r = i | |
| while l < r - 1: | |
| m = (l + r) // 2 | |
| if sd[m][0] <= d - sd[i][0]: | |
| l = m | |
| else: | |
| r = m | |
| ans3 = max(ans3, sd[i][1] + sdpref[l + 1]) | |
| ans = max(ans1, ans2, ans3) | |
| print(max(ans, 0)) | |
| ``` | |
| ### 005 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-3) | |
| ``` | |
| ### 006 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) & parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 007 | |
| ```python | |
| from bisect import * | |
| n, tc, td = [int(i) for i in input().split()] | |
| fc = [] | |
| fd = [] | |
| mbc = 0 | |
| mbd = 0 | |
| for _ in range(n): | |
| b, p, ft = input().split() | |
| b, p = (int(b), int(p)) | |
| f = (p, b) | |
| if ft == 'C': | |
| if p <= tc: | |
| fc.append(f) | |
| mbc = max(mbc, b) | |
| elif p <= td: | |
| fd.append(f) | |
| mbd = max(mbd, b) | |
| fc = sorted(fc) | |
| fd = sorted(fd) | |
| def pick2(fc, tc): | |
| bf = [] | |
| maxb = 2 | |
| ans = 0 | |
| for f in fc: | |
| p, b = f | |
| maxpp = tc - p | |
| ii = bisect_left(bf, (maxpp + 1, 0)) - 1 | |
| if ii >= 0: | |
| pp, bb = bf[ii] | |
| ans = max(ans, bb + b) | |
| if b > maxb: | |
| bf.append(f) | |
| maxb = b | |
| return ans | |
| ans = mbc + mbd if mbc > 0 and mbd > 0 else 0 | |
| ans = max(ans, pick2(fc, tc)) | |
| ans = max(ans, pick2(fd, td)) | |
| print(ans) | |
| ``` | |
| ### 008 | |
| ```python | |
| from bisect import * | |
| n, tc, td = [int(i) for i in input().split()] | |
| fc = [] | |
| fd = [] | |
| mbc = 0 | |
| mbd = 0 | |
| for _ in range(n): | |
| b, p, ft = input().split() | |
| b, p = (int(b), int(p)) | |
| f = (p, b) | |
| if ft == 'C': | |
| if p <= tc: | |
| fc.append(f) | |
| mbc = max(mbc, b) | |
| elif p < td: | |
| fd.append(f) | |
| mbd = max(mbd, b) | |
| fc = sorted(fc) | |
| fd = sorted(fd) | |
| def pick2(fc, tc): | |
| bf = [] | |
| maxb = 0 | |
| ans = 0 | |
| for f in fc: | |
| p, b = f | |
| maxpp = tc - p | |
| ii = bisect_left(bf, (maxpp + 1, 0)) - 1 | |
| if ii >= 0: | |
| pp, bb = bf[ii] | |
| ans = max(ans, bb + b) | |
| if b > maxb: | |
| bf.append(f) | |
| maxb = b | |
| return ans | |
| ans = mbc + mbd if mbc > 0 and mbd > 0 else 0 | |
| ans = max(ans, pick2(fc, tc)) | |
| ans = max(ans, pick2(fd, td)) | |
| print(ans) | |
| ``` | |
| ### 009 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 7) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 010 | |
| ```python | |
| from operator import itemgetter | |
| def get_one_max(arr, resource): | |
| res = 0 | |
| for beauty, _ in [x for x in arr if x[1] <= resource]: | |
| res = max(res, beauty) | |
| return res | |
| def get_two_max(arr, resource): | |
| arr.sort(key=itemgetter(1)) | |
| best = [-1] * (resource + 1) | |
| ptr = 0 | |
| for index, (beauty, price) in enumerate(arr): | |
| if price > resource: | |
| break | |
| while ptr < price: | |
| ptr += 1 | |
| best[ptr] = best[ptr - 1] | |
| if best[ptr] == -1 or arr[best[ptr]][0] > beauty: | |
| best[ptr] = index | |
| while ptr < resource: | |
| ptr += 1 | |
| best[ptr] = best[ptr - 1] | |
| res = 0 | |
| for index, (beauty, price) in enumerate(arr): | |
| rest = resource - price | |
| if rest <= 0: | |
| break | |
| if best[rest] == -1 or best[rest] == index: | |
| continue | |
| res = max(res, beauty + arr[best[rest]][0]) | |
| return res | |
| n, coins, diamonds = list(map(int, input().split())) | |
| with_coins = [] | |
| with_diamonds = [] | |
| for i in range(n): | |
| beauty, price, tp = input().split() | |
| if tp == 'C': | |
| with_coins.append((int(beauty), int(price))) | |
| else: | |
| with_diamonds.append((int(beauty), int(price))) | |
| with_coins_max = get_one_max(with_coins, coins) | |
| with_diamonds_max = get_one_max(with_diamonds, diamonds) | |
| ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max | |
| ans = max(ans, get_two_max(with_coins, coins)) | |
| ans = max(ans, get_two_max(with_diamonds, diamonds)) | |
| print(ans) | |
| ``` | |
| ### 011 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1, n + 1 - 1): | |
| if testing[i] != None: | |
| continue | |
| score = 1 | |
| allscore = 1 | |
| testing[i] = 1 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 012 | |
| ```python | |
| from bisect import * | |
| n, tc, td = [int(i) for i in input().split()] | |
| fc = [] | |
| fd = [] | |
| mbc = 0 | |
| mbd = 0 | |
| for _ in range(n): | |
| b, p, ft = input().split() | |
| b, p = (int(b), int(p)) | |
| f = (p, b) | |
| if ft == 'C': | |
| if p <= tc: | |
| fc.append(f) | |
| mbc = max(mbc, b) | |
| elif p <= td: | |
| fd.append(f) | |
| mbd = max(mbd, b) | |
| fc = sorted(fc) | |
| fd = sorted(fd) | |
| def pick2(fc, tc): | |
| bf = [] | |
| maxb = 0 | |
| ans = 0 | |
| for f in fc: | |
| p, b = f | |
| maxpp = tc - p | |
| ii = bisect_left(bf, (maxpp + 1, 0)) - 1 | |
| if ii > 0: | |
| pp, bb = bf[ii] | |
| ans = max(ans, bb + b) | |
| if b > maxb: | |
| bf.append(f) | |
| maxb = b | |
| return ans | |
| ans = mbc + mbd if mbc > 0 and mbd > 0 else 0 | |
| ans = max(ans, pick2(fc, tc)) | |
| ans = max(ans, pick2(fd, td)) | |
| print(ans) | |
| ``` | |
| ### 013 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = min(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result += sums[key] - sums[parents[key] + 1] | |
| else: | |
| result += sums[key] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 014 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1 + 1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 015 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-3) | |
| ``` | |
| ### 016 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1 + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 017 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1 - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 018 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(2): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 019 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 5: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c + 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 2) | |
| print(res) | |
| ``` | |
| ### 020 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(2, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 021 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2]) | |
| WCHANGE = [] | |
| for i in range(1, m): | |
| if EDGE[i - 1][2] != EDGE[i][2]: | |
| WCHANGE.append(i) | |
| WCHANGE.append(m) | |
| Group = [i for i in range(n + 1)] | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x) - 1] = min(find(y), find(x)) | |
| NOW = 0 | |
| noneed = [0] * m | |
| ANS = 0 | |
| for wc in WCHANGE: | |
| for j in range(NOW, wc): | |
| if find(EDGE[j][0]) == find(EDGE[j][1]): | |
| noneed[j] = 1 | |
| for j in range(NOW, wc): | |
| if noneed[j] == 1: | |
| continue | |
| x, y, w = EDGE[j] | |
| if find(x) != find(y): | |
| Union(x, y) | |
| else: | |
| ANS += 1 | |
| NOW = wc | |
| print(ANS) | |
| ``` | |
| ### 022 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy + m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 023 | |
| ```python | |
| import sys | |
| def get(v): | |
| if v == prev[v]: | |
| return v | |
| prev[v] = get(prev[v]) | |
| return prev[v] | |
| def join(u, v): | |
| u = get(u) | |
| v = get(v) | |
| if u != v: | |
| if size[u] < size[v]: | |
| u, v = (v, u) | |
| prev[v] = u | |
| size[u] += size[v] | |
| n, m = map(int, sys.stdin.readline().split()) | |
| edges = [0] * m | |
| for i in range(m): | |
| u, v, w = map(int, sys.stdin.readline().split()) | |
| edges[i] = (u - 1, v - 3, w) | |
| edges.sort(key=lambda e: e[2]) | |
| prev = list(range(n)) | |
| size = [1] * n | |
| res = 0 | |
| l, r = (0, 1) | |
| while l < m: | |
| while r < m and edges[l][2] == edges[r][2]: | |
| r += 1 | |
| res += r - l | |
| for i in range(l, r): | |
| if get(edges[i][0]) == get(edges[i][1]): | |
| res -= 1 | |
| for i in range(l, r): | |
| if get(edges[i][0]) != get(edges[i][1]): | |
| join(edges[i][0], edges[i][1]) | |
| res -= 1 | |
| l = r | |
| print(res) | |
| ``` | |
| ### 024 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1, n + 1): | |
| if testing[i] != None: | |
| continue | |
| score = 0 | |
| allscore = 1 | |
| testing[i] = 1 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 025 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(--1) | |
| ``` | |
| ### 026 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 7) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 027 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244352 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 028 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 3 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 029 | |
| ```python | |
| from bisect import * | |
| n, tc, td = [int(i) for i in input().split()] | |
| fc = [] | |
| fd = [] | |
| mbc = 0 | |
| mbd = 0 | |
| for _ in range(n): | |
| b, p, ft = input().split() | |
| b, p = (int(b), int(p)) | |
| f = (p, b) | |
| if ft == 'C': | |
| if p <= tc: | |
| fc.append(f) | |
| mbc = max(mbc, b) | |
| elif p <= td: | |
| fd.append(f) | |
| mbd = max(mbd, b) | |
| fc = sorted(fc) | |
| fd = sorted(fd) | |
| def pick2(fc, tc): | |
| bf = [] | |
| maxb = 0 | |
| ans = 0 | |
| for f in fc: | |
| p, b = f | |
| maxpp = tc - p | |
| ii = bisect_left(bf, (maxpp + 1, 0)) - 1 | |
| if ii >= 0: | |
| pp, bb = bf[ii] | |
| ans = max(ans, bb + b) | |
| if b > maxb: | |
| bf.append(f) | |
| maxb = b | |
| return ans | |
| ans = mbc + mbd if mbc >= 0 and mbd > 0 else 0 | |
| ans = max(ans, pick2(fc, tc)) | |
| ans = max(ans, pick2(fd, td)) | |
| print(ans) | |
| ``` | |
| ### 030 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if not x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 031 | |
| ```python | |
| import sys | |
| def get(v): | |
| if v == prev[v]: | |
| return v | |
| prev[v] = get(prev[v]) | |
| return prev[v] | |
| def join(u, v): | |
| u = get(u) | |
| v = get(v) | |
| if u != v: | |
| if size[u] < size[v]: | |
| u, v = (v, u) | |
| prev[v] = u | |
| size[u] += size[v] | |
| n, m = map(int, sys.stdin.readline().split()) | |
| edges = [0] * m | |
| for i in range(m): | |
| u, v, w = map(int, sys.stdin.readline().split()) | |
| edges[i] = (u - 1, v - 1, w) | |
| edges.sort(key=lambda e: e[0]) | |
| prev = list(range(n)) | |
| size = [1] * n | |
| res = 0 | |
| l, r = (0, 1) | |
| while l < m: | |
| while r < m and edges[l][2] == edges[r][2]: | |
| r += 1 | |
| res += r - l | |
| for i in range(l, r): | |
| if get(edges[i][0]) == get(edges[i][1]): | |
| res -= 1 | |
| for i in range(l, r): | |
| if get(edges[i][0]) != get(edges[i][1]): | |
| join(edges[i][0], edges[i][1]) | |
| res -= 1 | |
| l = r | |
| print(res) | |
| ``` | |
| ### 032 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n & i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 033 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1 - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 034 | |
| ```python | |
| import sys | |
| def read_data(): | |
| n = int(next(sys.stdin)) | |
| prices = list(map(int, next(sys.stdin).split())) | |
| vocabulary = [] | |
| for i in range(n): | |
| vocabulary.append(next(sys.stdin).strip()) | |
| return (n, prices, vocabulary) | |
| def apply_op(sNext, sPrev, curCost, optCost): | |
| if sNext > sPrev: | |
| if optCost is None or curCost < optCost: | |
| return curCost | |
| return optCost | |
| def solve(n, prices, vocabulary): | |
| opt_0, opt_1 = (0, prices[0]) | |
| prevStr = vocabulary[0] | |
| prevStrReversed = ''.join(reversed(prevStr)) | |
| for curStr, curPrice in zip(vocabulary[1:], prices[1:]): | |
| curStrReversed = ''.join(reversed(curStr)) | |
| n_opt0, n_opt1 = (None, None) | |
| if opt_0 is not None: | |
| n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1) | |
| if opt_1 is not None: | |
| n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1) | |
| opt_0, opt_1 = (n_opt0, n_opt1) | |
| prevStr, prevStrReversed = (curStr, curStrReversed) | |
| if opt_0 is None and opt_1 is None: | |
| break | |
| if opt_0 is None: | |
| return opt_1 | |
| if opt_1 is None: | |
| return opt_0 | |
| return min(opt_0, opt_1) | |
| def __starting_point(): | |
| n, prices, vocabulary = read_data() | |
| sorting_cost = solve(n, prices, vocabulary) | |
| if sorting_cost != None: | |
| print(sorting_cost) | |
| else: | |
| print(-1) | |
| __starting_point() | |
| ``` | |
| ### 035 | |
| ```python | |
| import sys | |
| line = sys.stdin.readlines() | |
| n = int(line[0]) | |
| c = list(map(int, line[1].split())) | |
| no = (0, '') | |
| yes = (0, '') | |
| for i in range(n): | |
| s = line[i + 2].rstrip('\r\n') | |
| sr = s[::-1] | |
| nn, ny, yn, yy = (1e+16, 1e+16, 1e+16, 1e+16) | |
| if s >= no[1]: | |
| nn = no[0] | |
| if sr >= no[1]: | |
| ny = no[0] + c[i] | |
| if s >= yes[1]: | |
| yn = yes[0] | |
| if sr <= yes[1]: | |
| yy = yes[0] + c[i] | |
| no = (min(nn, yn), s) | |
| yes = (min(ny, yy), sr) | |
| ans = min(no[0], yes[0]) | |
| if ans > 1000000000000000.0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 036 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = 2 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 037 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p >= q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 038 | |
| ```python | |
| n, m = map(int, input().split()) | |
| class edge: | |
| def __init__(self, U, V, W): | |
| self.u = U | |
| self.v = V | |
| self.w = W | |
| l = [] | |
| for i in range(m): | |
| u, v, w = map(int, input().split()) | |
| l.append(edge(u, v, w)) | |
| l.sort(key=lambda x: x.w) | |
| f = [i for i in range(0, n + 1)] | |
| def r(a): | |
| if a != f[a]: | |
| f[a - 1] = r(f[a]) | |
| return f[a] | |
| cnt = 0 | |
| i = 0 | |
| while i < m: | |
| j = i | |
| while l[i].w == l[j].w: | |
| j = j + 1 | |
| if j == m: | |
| break | |
| num = 0 | |
| for k in range(i, j): | |
| x = r(l[k].u) | |
| y = r(l[k].v) | |
| if x != y: | |
| num = num + 1 | |
| for k in range(i, j): | |
| x = r(l[k].u) | |
| y = r(l[k].v) | |
| if x != y: | |
| f[y] = x | |
| num = num - 1 | |
| cnt = cnt + num | |
| i = j | |
| print(cnt) | |
| ``` | |
| ### 039 | |
| ```python | |
| class DisjoinSet: | |
| def __init__(self, n): | |
| self.n = n | |
| self.dad = [i for i in range(n + 1)] | |
| self.cant = [1] * (n + 1) | |
| def SetOf(self, n): | |
| if self.dad[n] == n: | |
| return n | |
| self.dad[n] = self.SetOf(self.dad[n]) | |
| return self.dad[n] | |
| def Merge(self, a, b): | |
| a, b = (self.SetOf(a), self.SetOf(b)) | |
| if a != b: | |
| if self.cant[b] > self.cant[a]: | |
| a, b = (b, a) | |
| self.cant[a] += self.cant[b] | |
| self.dad[b] = a | |
| return True | |
| return False | |
| def Kruskal(l, n): | |
| l.sort() | |
| i = 0 | |
| ds = DisjoinSet(n) | |
| cant = 0 | |
| change = 0 | |
| while cant < n - 3 and i < len(l): | |
| val = l[i][0] | |
| old = [] | |
| while i < len(l) and val == l[i][0]: | |
| x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2])) | |
| if x != y: | |
| old.append((x, y)) | |
| i += 1 | |
| change += len(old) | |
| for x, y in old: | |
| change -= ds.Merge(x, y) | |
| return change | |
| n, m = input().split() | |
| n, m = (int(n), int(m)) | |
| e = [] | |
| for i in range(m): | |
| x, y, c = input().split() | |
| e.append((int(c), int(x), int(y))) | |
| print(Kruskal(e, n)) | |
| ``` | |
| ### 040 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s >= s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 041 | |
| ```python | |
| import sys | |
| def read_data(): | |
| n = int(next(sys.stdin)) | |
| prices = list(map(int, next(sys.stdin).split())) | |
| vocabulary = [] | |
| for i in range(n): | |
| vocabulary.append(next(sys.stdin).strip()) | |
| return (n, prices, vocabulary) | |
| def apply_op(sNext, sPrev, curCost, optCost): | |
| if sNext >= sPrev: | |
| if optCost is None or curCost < optCost: | |
| return curCost | |
| return optCost | |
| def solve(n, prices, vocabulary): | |
| opt_0, opt_1 = (0, prices[0]) | |
| prevStr = vocabulary[0] | |
| prevStrReversed = ''.join(reversed(prevStr)) | |
| for curStr, curPrice in zip(vocabulary[1:], prices[1:]): | |
| curStrReversed = ''.join(reversed(curStr)) | |
| n_opt0, n_opt1 = (None, None) | |
| if opt_0 is not None: | |
| n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1) | |
| if opt_1 is not None: | |
| n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1) | |
| opt_0, opt_1 = (n_opt0, n_opt1) | |
| prevStr, prevStrReversed = (curStr, curStrReversed) | |
| if opt_0 is None and opt_1 is None: | |
| break | |
| if opt_0 is None: | |
| return opt_1 | |
| if opt_1 is None: | |
| return opt_0 | |
| return min(opt_0, opt_1) | |
| def __starting_point(): | |
| n, prices, vocabulary = read_data() | |
| sorting_cost = solve(n, prices, vocabulary) | |
| if sorting_cost != None: | |
| print(sorting_cost) | |
| else: | |
| print(--1) | |
| __starting_point() | |
| ``` | |
| ### 042 | |
| ```python | |
| import sys | |
| line = sys.stdin.readlines() | |
| n = int(line[0]) | |
| c = list(map(int, line[1].split())) | |
| no = (0, '') | |
| yes = (0, '') | |
| for i in range(n): | |
| s = line[i + 2].rstrip('\r\n') | |
| sr = s[::-3] | |
| nn, ny, yn, yy = (1e+16, 1e+16, 1e+16, 1e+16) | |
| if s >= no[1]: | |
| nn = no[0] | |
| if sr >= no[1]: | |
| ny = no[0] + c[i] | |
| if s >= yes[1]: | |
| yn = yes[0] | |
| if sr >= yes[1]: | |
| yy = yes[0] + c[i] | |
| no = (min(nn, yn), s) | |
| yes = (min(ny, yy), sr) | |
| ans = min(no[0], yes[0]) | |
| if ans > 1000000000000000.0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 043 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[1] - q[0]) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si + a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 0 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 044 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1] < suma[vertex - 1 - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 045 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 1: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 046 | |
| ```python | |
| n = int(input()) | |
| c = [int(s) for s in input().split(' ')] | |
| C_rev = c[0] | |
| C_nrev = 0 | |
| last_s = input() | |
| last_s_rev = last_s[::+1] | |
| for i in range(1, n): | |
| s = input() | |
| s_rev = s[::-1] | |
| new_C_nrev = float('inf') | |
| if last_s <= s: | |
| new_C_nrev = min(new_C_nrev, C_nrev) | |
| if last_s_rev <= s: | |
| new_C_nrev = min(new_C_nrev, C_rev) | |
| new_C_rev = float('inf') | |
| if last_s <= s_rev: | |
| new_C_rev = min(new_C_rev, C_nrev + c[i]) | |
| if last_s_rev <= s_rev: | |
| new_C_rev = min(new_C_rev, C_rev + c[i]) | |
| if min(C_rev, C_nrev) == float('inf'): | |
| break | |
| C_rev = new_C_rev | |
| C_nrev = new_C_nrev | |
| last_s = s | |
| last_s_rev = s_rev | |
| print(-1 if min(C_rev, C_nrev) == float('inf') else min(C_rev, C_nrev)) | |
| ``` | |
| ### 047 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1 + 1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 048 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-0) | |
| ``` | |
| ### 049 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < 0: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = min([s[k - 1] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i])) | |
| print(ans) | |
| ``` | |
| ### 050 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(5, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 051 | |
| ```python | |
| from operator import itemgetter | |
| def get_one_max(arr, resource): | |
| res = 0 | |
| for beauty, _ in [x for x in arr if x[1] <= resource]: | |
| res = max(res, beauty) | |
| return res | |
| def get_two_max(arr, resource): | |
| arr.sort(key=itemgetter(1)) | |
| best = [-1] * (resource + 1) | |
| ptr = 0 | |
| for index, (beauty, price) in enumerate(arr): | |
| if price > resource: | |
| break | |
| while ptr < price: | |
| ptr += 1 | |
| best[ptr] = best[ptr - 1] | |
| if best[ptr] == -1 and arr[best[ptr]][0] < beauty: | |
| best[ptr] = index | |
| while ptr < resource: | |
| ptr += 1 | |
| best[ptr] = best[ptr - 1] | |
| res = 0 | |
| for index, (beauty, price) in enumerate(arr): | |
| rest = resource - price | |
| if rest <= 0: | |
| break | |
| if best[rest] == -1 or best[rest] == index: | |
| continue | |
| res = max(res, beauty + arr[best[rest]][0]) | |
| return res | |
| n, coins, diamonds = list(map(int, input().split())) | |
| with_coins = [] | |
| with_diamonds = [] | |
| for i in range(n): | |
| beauty, price, tp = input().split() | |
| if tp == 'C': | |
| with_coins.append((int(beauty), int(price))) | |
| else: | |
| with_diamonds.append((int(beauty), int(price))) | |
| with_coins_max = get_one_max(with_coins, coins) | |
| with_diamonds_max = get_one_max(with_diamonds, diamonds) | |
| ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max | |
| ans = max(ans, get_two_max(with_coins, coins)) | |
| ans = max(ans, get_two_max(with_diamonds, diamonds)) | |
| print(ans) | |
| ``` | |
| ### 052 | |
| ```python | |
| import sys | |
| def get(v): | |
| if v == prev[v]: | |
| return v | |
| prev[v] = get(prev[v]) | |
| return prev[v] | |
| def join(u, v): | |
| u = get(u) | |
| v = get(v) | |
| if u != v: | |
| if size[u] < size[v]: | |
| u, v = (v, u) | |
| prev[v] = u | |
| size[u] += size[v] | |
| n, m = map(int, sys.stdin.readline().split()) | |
| edges = [0] * m | |
| for i in range(m): | |
| u, v, w = map(int, sys.stdin.readline().split()) | |
| edges[i] = (u - 1, v - 1, w) | |
| edges.sort(key=lambda e: e[2]) | |
| prev = list(range(n)) | |
| size = [1] * n | |
| res = 0 | |
| l, r = (0, 1) | |
| while l < m: | |
| while r < m and edges[l][2] == edges[r][2]: | |
| r += 1 | |
| res += r - l | |
| for i in range(l, r): | |
| if get(edges[i][0]) == get(edges[i][1]): | |
| res -= -1 | |
| for i in range(l, r): | |
| if get(edges[i][0]) != get(edges[i][1]): | |
| join(edges[i][0], edges[i][1]) | |
| res -= 1 | |
| l = r | |
| print(res) | |
| ``` | |
| ### 053 | |
| ```python | |
| import sys | |
| def read_data(): | |
| n = int(next(sys.stdin)) | |
| prices = list(map(int, next(sys.stdin).split())) | |
| vocabulary = [] | |
| for i in range(n): | |
| vocabulary.append(next(sys.stdin).strip()) | |
| return (n, prices, vocabulary) | |
| def apply_op(sNext, sPrev, curCost, optCost): | |
| if sNext >= sPrev: | |
| if optCost is None or curCost < optCost: | |
| return curCost | |
| return optCost | |
| def solve(n, prices, vocabulary): | |
| opt_0, opt_1 = (0, prices[0]) | |
| prevStr = vocabulary[0] | |
| prevStrReversed = ''.join(reversed(prevStr)) | |
| for curStr, curPrice in zip(vocabulary[1:], prices[1:]): | |
| curStrReversed = ''.join(reversed(curStr)) | |
| n_opt0, n_opt1 = (None, None) | |
| if opt_0 is not None: | |
| n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1) | |
| if opt_1 is not None: | |
| n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1) | |
| opt_0, opt_1 = (n_opt0, n_opt1) | |
| prevStr, prevStrReversed = (curStr, curStrReversed) | |
| if opt_0 is None and opt_1 is None: | |
| break | |
| if opt_0 is None: | |
| return opt_1 | |
| if opt_1 is None: | |
| return opt_0 | |
| return min(opt_0, opt_1) | |
| def __starting_point(): | |
| n, prices, vocabulary = read_data() | |
| sorting_cost = solve(n, prices, vocabulary) | |
| if sorting_cost != None: | |
| print(sorting_cost) | |
| else: | |
| print(-1) | |
| __starting_point() | |
| ``` | |
| ### 054 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] >= hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 055 | |
| ```python | |
| n, m = map(int, input().split()) | |
| class edge: | |
| def __init__(self, U, V, W): | |
| self.u = U | |
| self.v = V | |
| self.w = W | |
| l = [] | |
| for i in range(m): | |
| u, v, w = map(int, input().split()) | |
| l.append(edge(u, v, w)) | |
| l.sort(key=lambda x: x.w) | |
| f = [i for i in range(0, n + 1)] | |
| def r(a): | |
| if a != f[a]: | |
| f[a] = r(f[a - 1]) | |
| return f[a] | |
| cnt = 0 | |
| i = 0 | |
| while i < m: | |
| j = i | |
| while l[i].w == l[j].w: | |
| j = j + 1 | |
| if j == m: | |
| break | |
| num = 0 | |
| for k in range(i, j): | |
| x = r(l[k].u) | |
| y = r(l[k].v) | |
| if x != y: | |
| num = num + 1 | |
| for k in range(i, j): | |
| x = r(l[k].u) | |
| y = r(l[k].v) | |
| if x != y: | |
| f[y] = x | |
| num = num - 1 | |
| cnt = cnt + num | |
| i = j | |
| print(cnt) | |
| ``` | |
| ### 056 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if (A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1]))) and (A_[1][1] and A_[4][1] and A_[7][1]) and (A_[2][1] and A_[5][1] and A_[8][1]) and (A_[3][1] and A_[4][1] and A_[5][1]) and (A_[6][1] and A_[7][1] and A_[8][1]) and (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 057 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S + tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 0: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h > 0: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 058 | |
| ```python | |
| class DisjoinSet: | |
| def __init__(self, n): | |
| self.n = n | |
| self.dad = [i for i in range(n + 1)] | |
| self.cant = [1] * (n + 1) | |
| def SetOf(self, n): | |
| if self.dad[n] == n: | |
| return n | |
| self.dad[n] = self.SetOf(self.dad[n]) | |
| return self.dad[n] | |
| def Merge(self, a, b): | |
| a, b = (self.SetOf(a), self.SetOf(b)) | |
| if a != b: | |
| if self.cant[b] > self.cant[a]: | |
| a, b = (b, a) | |
| self.cant[a] += self.cant[b] | |
| self.dad[b] = a | |
| return True | |
| return False | |
| def Kruskal(l, n): | |
| l.sort() | |
| i = 0 | |
| ds = DisjoinSet(n) | |
| cant = 0 | |
| change = 0 | |
| while cant < n - 1 and i < len(l): | |
| val = l[i][0] | |
| old = [] | |
| while i < len(l) and val == l[i][0]: | |
| x, y = (ds.SetOf(l[i][-3]), ds.SetOf(l[i][2])) | |
| if x != y: | |
| old.append((x, y)) | |
| i += 1 | |
| change += len(old) | |
| for x, y in old: | |
| change -= ds.Merge(x, y) | |
| return change | |
| n, m = input().split() | |
| n, m = (int(n), int(m)) | |
| e = [] | |
| for i in range(m): | |
| x, y, c = input().split() | |
| e.append((int(c), int(x), int(y))) | |
| print(Kruskal(e, n)) | |
| ``` | |
| ### 059 | |
| ```python | |
| class DisjoinSet: | |
| def __init__(self, n): | |
| self.n = n | |
| self.dad = [i for i in range(n + 1)] | |
| self.cant = [1] * (n + 1) | |
| def SetOf(self, n): | |
| if self.dad[n] == n: | |
| return n | |
| self.dad[n] = self.SetOf(self.dad[n]) | |
| return self.dad[n] | |
| def Merge(self, a, b): | |
| a, b = (self.SetOf(a), self.SetOf(b)) | |
| if a != b: | |
| if self.cant[b] > self.cant[a]: | |
| a, b = (b, a) | |
| self.cant[a] += self.cant[b] | |
| self.dad[b] = a | |
| return True | |
| return False | |
| def Kruskal(l, n): | |
| l.sort() | |
| i = 0 | |
| ds = DisjoinSet(n) | |
| cant = 0 | |
| change = 0 | |
| while cant < n - 5 and i < len(l): | |
| val = l[i][0] | |
| old = [] | |
| while i < len(l) and val == l[i][0]: | |
| x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2])) | |
| if x != y: | |
| old.append((x, y)) | |
| i += 1 | |
| change += len(old) | |
| for x, y in old: | |
| change -= ds.Merge(x, y) | |
| return change | |
| n, m = input().split() | |
| n, m = (int(n), int(m)) | |
| e = [] | |
| for i in range(m): | |
| x, y, c = input().split() | |
| e.append((int(c), int(x), int(y))) | |
| print(Kruskal(e, n)) | |
| ``` | |
| ### 060 | |
| ```python | |
| n, c, d = map(int, input().split()) | |
| inf = 10 ** 9 | |
| a = [0] * n | |
| sc = [] | |
| sd = [] | |
| for i in range(n): | |
| b, p, t = input().split() | |
| b = int(b) | |
| p = int(p) | |
| a[i] = [b, p, t] | |
| if t == 'C': | |
| sc.append([p, b]) | |
| else: | |
| sd.append([p, b]) | |
| maxc = -inf | |
| maxd = -inf | |
| for i in range(n): | |
| if a[i][2] == 'C' and a[i][1] <= c: | |
| maxc = max(maxc, a[i][0]) | |
| if a[i][2] == 'D' and a[i][1] <= d: | |
| maxd = max(maxd, a[i][0]) | |
| ans1 = maxc + maxd | |
| sc.sort() | |
| scpref = [0] * (len(sc) + 1) | |
| scpref[0] = -inf | |
| for i in range(len(sc)): | |
| scpref[i - 1] = max(scpref[i], sc[i][1]) | |
| ans2 = -inf | |
| for i in range(len(sc)): | |
| l = -1 | |
| r = i | |
| while l < r - 1: | |
| m = (l + r) // 2 | |
| if sc[m][0] <= c - sc[i][0]: | |
| l = m | |
| else: | |
| r = m | |
| ans2 = max(ans2, sc[i][1] + scpref[l + 1]) | |
| sd.sort() | |
| sdpref = [0] * (len(sd) + 1) | |
| sdpref[0] = -inf | |
| for i in range(len(sd)): | |
| sdpref[i + 1] = max(sdpref[i], sd[i][1]) | |
| ans3 = -inf | |
| for i in range(len(sd)): | |
| l = -1 | |
| r = i | |
| while l < r - 1: | |
| m = (l + r) // 2 | |
| if sd[m][0] <= d - sd[i][0]: | |
| l = m | |
| else: | |
| r = m | |
| ans3 = max(ans3, sd[i][1] + sdpref[l + 1]) | |
| ans = max(ans1, ans2, ans3) | |
| print(max(ans, 0)) | |
| ``` | |
| ### 061 | |
| ```python | |
| n = int(input()) | |
| c = [int(s) for s in input().split(' ')] | |
| C_rev = c[0] | |
| C_nrev = 0 | |
| last_s = input() | |
| last_s_rev = last_s[::-3] | |
| for i in range(1, n): | |
| s = input() | |
| s_rev = s[::-1] | |
| new_C_nrev = float('inf') | |
| if last_s <= s: | |
| new_C_nrev = min(new_C_nrev, C_nrev) | |
| if last_s_rev <= s: | |
| new_C_nrev = min(new_C_nrev, C_rev) | |
| new_C_rev = float('inf') | |
| if last_s <= s_rev: | |
| new_C_rev = min(new_C_rev, C_nrev + c[i]) | |
| if last_s_rev <= s_rev: | |
| new_C_rev = min(new_C_rev, C_rev + c[i]) | |
| if min(C_rev, C_nrev) == float('inf'): | |
| break | |
| C_rev = new_C_rev | |
| C_nrev = new_C_nrev | |
| last_s = s | |
| last_s_rev = s_rev | |
| print(-1 if min(C_rev, C_nrev) == float('inf') else min(C_rev, C_nrev)) | |
| ``` | |
| ### 062 | |
| ```python | |
| import math as m | |
| n = int(input()) | |
| cost = list(map(int, input().split())) | |
| s = [] | |
| sr = [] | |
| for i in range(n): | |
| s.append(list(input())) | |
| sr.append(list(reversed(s[i]))) | |
| dp = [[0, 0] for i in range(n + 1)] | |
| flag = 0 | |
| dp[0][1] = cost[0] | |
| for i in range(1, n): | |
| if dp[i - 1][0] == -1 and dp[i - 1][0] == -1: | |
| flag = 1 | |
| break | |
| elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1: | |
| if s[i] >= s[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1: | |
| if s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| else: | |
| if s[i] >= s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) | |
| elif s[i] >= s[i - 1] and s[i] < sr[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| elif s[i] < s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i] | |
| elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| if flag == 1: | |
| print(-1) | |
| else: | |
| res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1]) | |
| print(res) | |
| ``` | |
| ### 063 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][1] and A_[7][1]) or (A_[2][1] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][0] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 064 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 1: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 065 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = (a_3_3[0][index] + a_3_3[1][index]) * a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 0 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 066 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i >= 0 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 067 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 4 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 068 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i - 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 069 | |
| ```python | |
| from operator import itemgetter | |
| def get_one_max(arr, resource): | |
| res = 0 | |
| for beauty, _ in [x for x in arr if x[1] <= resource]: | |
| res = max(res, beauty) | |
| return res | |
| def get_two_max(arr, resource): | |
| arr.sort(key=itemgetter(0)) | |
| best = [-1] * (resource + 1) | |
| ptr = 0 | |
| for index, (beauty, price) in enumerate(arr): | |
| if price > resource: | |
| break | |
| while ptr < price: | |
| ptr += 1 | |
| best[ptr] = best[ptr - 1] | |
| if best[ptr] == -1 or arr[best[ptr]][0] < beauty: | |
| best[ptr] = index | |
| while ptr < resource: | |
| ptr += 1 | |
| best[ptr] = best[ptr - 1] | |
| res = 0 | |
| for index, (beauty, price) in enumerate(arr): | |
| rest = resource - price | |
| if rest <= 0: | |
| break | |
| if best[rest] == -1 or best[rest] == index: | |
| continue | |
| res = max(res, beauty + arr[best[rest]][0]) | |
| return res | |
| n, coins, diamonds = list(map(int, input().split())) | |
| with_coins = [] | |
| with_diamonds = [] | |
| for i in range(n): | |
| beauty, price, tp = input().split() | |
| if tp == 'C': | |
| with_coins.append((int(beauty), int(price))) | |
| else: | |
| with_diamonds.append((int(beauty), int(price))) | |
| with_coins_max = get_one_max(with_coins, coins) | |
| with_diamonds_max = get_one_max(with_diamonds, diamonds) | |
| ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max | |
| ans = max(ans, get_two_max(with_coins, coins)) | |
| ans = max(ans, get_two_max(with_diamonds, diamonds)) | |
| print(ans) | |
| ``` | |
| ### 070 | |
| ```python | |
| import sys | |
| def get(v): | |
| if v == prev[v]: | |
| return v | |
| prev[v] = get(prev[v]) | |
| return prev[v] | |
| def join(u, v): | |
| u = get(u) | |
| v = get(v) | |
| if u != v: | |
| if size[u] < size[v]: | |
| u, v = (v, u) | |
| prev[v] = u | |
| size[u] += size[v] | |
| n, m = map(int, sys.stdin.readline().split()) | |
| edges = [0] * m | |
| for i in range(m): | |
| u, v, w = map(int, sys.stdin.readline().split()) | |
| edges[i] = (u - 1, v - 1, w) | |
| edges.sort(key=lambda e: e[2]) | |
| prev = list(range(n)) | |
| size = [1] * n | |
| res = 0 | |
| l, r = (0, 1) | |
| while l < m: | |
| while r < m and edges[l][2] == edges[r][2]: | |
| r += 1 | |
| res += r - l | |
| for i in range(l, r): | |
| if get(edges[i][0]) == get(edges[i][1]): | |
| res -= 0 | |
| for i in range(l, r): | |
| if get(edges[i][0]) != get(edges[i][1]): | |
| join(edges[i][0], edges[i][1]) | |
| res -= 1 | |
| l = r | |
| print(res) | |
| ``` | |
| ### 071 | |
| ```python | |
| n, c, d = map(int, input().split()) | |
| inf = 10 ** 9 | |
| a = [0] * n | |
| sc = [] | |
| sd = [] | |
| for i in range(n): | |
| b, p, t = input().split() | |
| b = int(b) | |
| p = int(p) | |
| a[i] = [b, p, t] | |
| if t == 'C': | |
| sc.append([p, b]) | |
| else: | |
| sd.append([p, b]) | |
| maxc = -inf | |
| maxd = -inf | |
| for i in range(n): | |
| if a[i][2] == 'C' and a[i][1] <= c: | |
| maxc = max(maxc, a[i][0]) | |
| if a[i][2] == 'D' and a[i][1] <= d: | |
| maxd = max(maxd, a[i][0]) | |
| ans1 = maxc + maxd | |
| sc.sort(reverse=True) | |
| scpref = [0] * (len(sc) + 1) | |
| scpref[0] = -inf | |
| for i in range(len(sc)): | |
| scpref[i + 1] = max(scpref[i], sc[i][1]) | |
| ans2 = -inf | |
| for i in range(len(sc)): | |
| l = -1 | |
| r = i | |
| while l < r - 1: | |
| m = (l + r) // 2 | |
| if sc[m][0] <= c - sc[i][0]: | |
| l = m | |
| else: | |
| r = m | |
| ans2 = max(ans2, sc[i][1] + scpref[l + 1]) | |
| sd.sort() | |
| sdpref = [0] * (len(sd) + 1) | |
| sdpref[0] = -inf | |
| for i in range(len(sd)): | |
| sdpref[i + 1] = max(sdpref[i], sd[i][1]) | |
| ans3 = -inf | |
| for i in range(len(sd)): | |
| l = -1 | |
| r = i | |
| while l < r - 1: | |
| m = (l + r) // 2 | |
| if sd[m][0] <= d - sd[i][0]: | |
| l = m | |
| else: | |
| r = m | |
| ans3 = max(ans3, sd[i][1] + sdpref[l + 1]) | |
| ans = max(ans1, ans2, ans3) | |
| print(max(ans, 0)) | |
| ``` | |
| ### 072 | |
| ```python | |
| import math as m | |
| n = int(input()) | |
| cost = list(map(int, input().split())) | |
| s = [] | |
| sr = [] | |
| for i in range(n): | |
| s.append(list(input())) | |
| sr.append(list(reversed(s[i]))) | |
| dp = [[2, 0] for i in range(n + 1)] | |
| flag = 0 | |
| dp[0][1] = cost[0] | |
| for i in range(1, n): | |
| if dp[i - 1][0] == -1 and dp[i - 1][1] == -1: | |
| flag = 1 | |
| break | |
| elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1: | |
| if s[i] >= s[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1: | |
| if s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| else: | |
| if s[i] >= s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) | |
| elif s[i] >= s[i - 1] and s[i] < sr[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| elif s[i] < s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i] | |
| elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| if flag == 1: | |
| print(-1) | |
| else: | |
| res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1]) | |
| print(res) | |
| ``` | |
| ### 073 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], -1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 074 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 075 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return -2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 076 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][1] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 077 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1 - 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 078 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v * d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 079 | |
| ```python | |
| n, t, d = map(int, input().split()) | |
| coin = [[(0, 0), (0, 0)] for i in range(100001)] | |
| diam = [[(0, 0), (0, 0)] for i in range(100001)] | |
| f = [] | |
| for i in range(n): | |
| b, c, q = input().split() | |
| b = int(b) | |
| c = int(c) | |
| if q == 'C': | |
| f.append((b, c, 1)) | |
| if coin[c][0][0] < b: | |
| coin[c][1] = coin[c][0] | |
| coin[c][0] = (b, i) | |
| elif coin[c][1][0] < b: | |
| coin[c][1] = (b, i) | |
| else: | |
| f.append((b, c, 2)) | |
| if diam[c][0][0] < b: | |
| diam[c][1] = diam[c][0] | |
| diam[c][0] = (b, i) | |
| elif diam[c][1][0] < b: | |
| diam[c][1] = (b, i) | |
| for i in range(2, 100001): | |
| if coin[i][0][0] < coin[i - 1][0][0]: | |
| coin[i][1] = max(coin[i][0], coin[i - 1][1]) | |
| coin[i][0] = coin[i - 1][0] | |
| else: | |
| coin[i][1] = max(coin[i - 1][1], coin[i][1]) | |
| if diam[i][0][0] < diam[i - 1][0][0]: | |
| diam[i][1] = max(diam[i][0], diam[i - 1][1]) | |
| diam[i][0] = diam[i - 1][0] | |
| else: | |
| diam[i][1] = max(diam[i - 1][1], diam[i][1]) | |
| p = False | |
| ans = 0 | |
| for i in range(n): | |
| fnt = f[i] | |
| if fnt[2] == 1: | |
| if t >= fnt[1]: | |
| s = t - fnt[1] | |
| if coin[s][0][0] > 0 and coin[s][0][1] != i: | |
| ans = max(fnt[0] + coin[s][0][0], ans) | |
| p = True | |
| elif coin[s][1][0] > 0: | |
| ans = max(fnt[0] + coin[s][1][0], ans) | |
| p = True | |
| if diam[d][0][0] > 0: | |
| ans = max(fnt[0] + diam[d][0][0], ans) | |
| p = True | |
| elif d >= fnt[1]: | |
| s = d - fnt[1] | |
| if diam[s][0][0] > 0 and diam[s][0][1] != i: | |
| ans = max(fnt[0] + diam[s][0][0], ans) | |
| p = True | |
| elif diam[s][1][0] > 0: | |
| ans = max(fnt[0] + diam[s][1][0], ans) | |
| p = True | |
| if coin[t][0][0] > 0: | |
| ans = max(fnt[0] + coin[t][0][0], ans) | |
| p = True | |
| if p: | |
| print(ans) | |
| else: | |
| print(3) | |
| ``` | |
| ### 080 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans //= pow(3, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 081 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-4) | |
| ``` | |
| ### 082 | |
| ```python | |
| class DisjoinSet: | |
| def __init__(self, n): | |
| self.n = n | |
| self.dad = [i for i in range(n + 1)] | |
| self.cant = [1] * (n + 1) | |
| def SetOf(self, n): | |
| if self.dad[n] == n: | |
| return n | |
| self.dad[n] = self.SetOf(self.dad[n]) | |
| return self.dad[n] | |
| def Merge(self, a, b): | |
| a, b = (self.SetOf(a), self.SetOf(b)) | |
| if a != b: | |
| if self.cant[b] > self.cant[a]: | |
| a, b = (b, a) | |
| self.cant[a] += self.cant[b] | |
| self.dad[b] = a | |
| return True | |
| return False | |
| def Kruskal(l, n): | |
| l.sort() | |
| i = -2 | |
| ds = DisjoinSet(n) | |
| cant = 0 | |
| change = 0 | |
| while cant < n - 1 and i < len(l): | |
| val = l[i][0] | |
| old = [] | |
| while i < len(l) and val == l[i][0]: | |
| x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2])) | |
| if x != y: | |
| old.append((x, y)) | |
| i += 1 | |
| change += len(old) | |
| for x, y in old: | |
| change -= ds.Merge(x, y) | |
| return change | |
| n, m = input().split() | |
| n, m = (int(n), int(m)) | |
| e = [] | |
| for i in range(m): | |
| x, y, c = input().split() | |
| e.append((int(c), int(x), int(y))) | |
| print(Kruskal(e, n)) | |
| ``` | |
| ### 083 | |
| ```python | |
| n, t, d = map(int, input().split()) | |
| coin = [[(0, 0), (0, 0)] for i in range(100001)] | |
| diam = [[(0, 0), (0, 0)] for i in range(100001)] | |
| f = [] | |
| for i in range(n): | |
| b, c, q = input().split() | |
| b = int(b) | |
| c = int(c) | |
| if q == 'C': | |
| f.append((b, c, 1)) | |
| if coin[c][0][0] < b: | |
| coin[c][1] = coin[c][0] | |
| coin[c][0] = (b, i) | |
| elif coin[c][1][0] < b: | |
| coin[c][1] = (b, i) | |
| else: | |
| f.append((b, c, 2)) | |
| if diam[c][0][0] < b: | |
| diam[c][1] = diam[c][0] | |
| diam[c][0] = (b, i) | |
| elif diam[c][1][0] < b: | |
| diam[c][1] = (b, i) | |
| for i in range(2, 100001): | |
| if coin[i][0][0] < coin[i - 1][0][0]: | |
| coin[i][1] = max(coin[i][0], coin[i - 1][1]) | |
| coin[i][0] = coin[i - 1][0] | |
| else: | |
| coin[i][1] = max(coin[i - 1][1], coin[i][1]) | |
| if diam[i][0][0] < diam[i - 1][0][0]: | |
| diam[i][1] = max(diam[i][0], diam[i - 1][1]) | |
| diam[i][0] = diam[i - 1][0] | |
| else: | |
| diam[i][1] = max(diam[i - 1][1], diam[i][1]) | |
| p = False | |
| ans = 0 | |
| for i in range(n): | |
| fnt = f[i] | |
| if fnt[2] == 1: | |
| if t >= fnt[1]: | |
| s = t - fnt[1] | |
| if coin[s][0][0] > 0 and coin[s][0][1] != i: | |
| ans = max(fnt[0] + coin[s][0][0], ans) | |
| p = True | |
| elif coin[s][1][0] > 0: | |
| ans = max(fnt[0] + coin[s][1][0], ans) | |
| p = True | |
| if diam[d][0][0] > 0: | |
| ans = max(fnt[0] + diam[d][0][0], ans) | |
| p = True | |
| elif d >= fnt[1]: | |
| s = d - fnt[1] | |
| if diam[s][0][0] > 0 and diam[s][0][1] != i: | |
| ans = max(fnt[0] + diam[s][0][0], ans) | |
| p = True | |
| elif diam[s][1][0] > 0: | |
| ans = max(fnt[0] + diam[s][1][0], ans) | |
| p = True | |
| if coin[t][0][0] > 0: | |
| ans = max(fnt[0] + coin[t][0][0], ans) | |
| p = True | |
| if p: | |
| print(ans) | |
| else: | |
| print(-4) | |
| ``` | |
| ### 084 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244349 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(3, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 085 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1, n + 1): | |
| if testing[i - 1] != None: | |
| continue | |
| score = 1 | |
| allscore = 1 | |
| testing[i] = 1 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 086 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[1] - q[0]) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si * a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 1 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 087 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244353 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1 - 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 088 | |
| ```python | |
| n, t, d = map(int, input().split()) | |
| coin = [[(0, 0), (0, 0)] for i in range(100001)] | |
| diam = [[(0, 0), (0, 0)] for i in range(100001)] | |
| f = [] | |
| for i in range(n): | |
| b, c, q = input().split() | |
| b = int(b) | |
| c = int(c) | |
| if q == 'C': | |
| f.append((b, c, 1)) | |
| if coin[c][0][0] < b: | |
| coin[c][1] = coin[c][0] | |
| coin[c][0] = (b, i) | |
| elif coin[c][1][0] < b: | |
| coin[c][1] = (b, i) | |
| else: | |
| f.append((b, c, 2)) | |
| if diam[c][0][0] < b: | |
| diam[c][1] = diam[c][0] | |
| diam[c][0] = (b, i) | |
| elif diam[c][1][0] < b: | |
| diam[c][1] = (b, i) | |
| for i in range(2, 100001): | |
| if coin[i][0][0] < coin[i - 1][0][0]: | |
| coin[i][1] = max(coin[i][0], coin[i - 1][1]) | |
| coin[i][0] = coin[i - 1][0] | |
| else: | |
| coin[i][1] = max(coin[i - 1][1], coin[i][1]) | |
| if diam[i][0][0] < diam[i - 1][0][0]: | |
| diam[i][1] = max(diam[i][0], diam[i - 1][1]) | |
| diam[i][0] = diam[i - 1][0] | |
| else: | |
| diam[i][1] = max(diam[i - 1][1], diam[i][1]) | |
| p = False | |
| ans = 0 | |
| for i in range(n): | |
| fnt = f[i] | |
| if fnt[2] == 1: | |
| if t >= fnt[1]: | |
| s = t - fnt[1] | |
| if coin[s][0][0] > 0 and coin[s][0][1] != i: | |
| ans = max(fnt[0] + coin[s][0][0], ans) | |
| p = True | |
| elif coin[s][1][0] > 0: | |
| ans = max(fnt[0] + coin[s][1][0], ans) | |
| p = True | |
| if diam[d][0][0] > 0: | |
| ans = max(fnt[0] + diam[d][0][0], ans) | |
| p = True | |
| elif d >= fnt[1]: | |
| s = d - fnt[1] | |
| if diam[s][0][0] > 0 and diam[s][0][1] != i: | |
| ans = max(fnt[0] + diam[s][0][0], ans) | |
| p = True | |
| elif diam[s][1][0] > 0: | |
| ans = max(fnt[0] + diam[s][1][0], ans) | |
| p = True | |
| if coin[t][0][0] > 0: | |
| ans = max(fnt[0] + coin[t][0][0], ans) | |
| p = True | |
| if p: | |
| print(ans) | |
| else: | |
| print(2) | |
| ``` | |
| ### 089 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-4) | |
| ``` | |
| ### 090 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][-2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 091 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 1: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c + 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 6) | |
| print(res) | |
| ``` | |
| ### 092 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 1: | |
| ans *= 7 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 093 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 1: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 094 | |
| ```python | |
| from operator import itemgetter | |
| def get_one_max(arr, resource): | |
| res = 0 | |
| for beauty, _ in [x for x in arr if x[1] <= resource]: | |
| res = max(res, beauty) | |
| return res | |
| def get_two_max(arr, resource): | |
| arr.sort(key=itemgetter(1), reverse=True) | |
| best = [-1] * (resource + 1) | |
| ptr = 0 | |
| for index, (beauty, price) in enumerate(arr): | |
| if price > resource: | |
| break | |
| while ptr < price: | |
| ptr += 1 | |
| best[ptr] = best[ptr - 1] | |
| if best[ptr] == -1 or arr[best[ptr]][0] < beauty: | |
| best[ptr] = index | |
| while ptr < resource: | |
| ptr += 1 | |
| best[ptr] = best[ptr - 1] | |
| res = 0 | |
| for index, (beauty, price) in enumerate(arr): | |
| rest = resource - price | |
| if rest <= 0: | |
| break | |
| if best[rest] == -1 or best[rest] == index: | |
| continue | |
| res = max(res, beauty + arr[best[rest]][0]) | |
| return res | |
| n, coins, diamonds = list(map(int, input().split())) | |
| with_coins = [] | |
| with_diamonds = [] | |
| for i in range(n): | |
| beauty, price, tp = input().split() | |
| if tp == 'C': | |
| with_coins.append((int(beauty), int(price))) | |
| else: | |
| with_diamonds.append((int(beauty), int(price))) | |
| with_coins_max = get_one_max(with_coins, coins) | |
| with_diamonds_max = get_one_max(with_diamonds, diamonds) | |
| ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max | |
| ans = max(ans, get_two_max(with_coins, coins)) | |
| ans = max(ans, get_two_max(with_diamonds, diamonds)) | |
| print(ans) | |
| ``` | |
| ### 095 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 1): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 096 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 3: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c + 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 2) | |
| print(res) | |
| ``` | |
| ### 097 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 1 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 098 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 2 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 099 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 100 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 101 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, -1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 102 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 103 | |
| ```python | |
| import sys | |
| line = sys.stdin.readlines() | |
| n = int(line[0]) | |
| c = list(map(int, line[1].split())) | |
| no = (0, '') | |
| yes = (0, '') | |
| for i in range(n): | |
| s = line[i + 2].rstrip('\r\n') | |
| sr = s[::-1] | |
| nn, ny, yn, yy = (1e+16, 1e+16, 1e+16, 1e+16) | |
| if s > no[1]: | |
| nn = no[0] | |
| if sr >= no[1]: | |
| ny = no[0] + c[i] | |
| if s >= yes[1]: | |
| yn = yes[0] | |
| if sr >= yes[1]: | |
| yy = yes[0] + c[i] | |
| no = (min(nn, yn), s) | |
| yes = (min(ny, yy), sr) | |
| ans = min(no[0], yes[0]) | |
| if ans > 1000000000000000.0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 104 | |
| ```python | |
| from bisect import * | |
| n, tc, td = [int(i) for i in input().split()] | |
| fc = [] | |
| fd = [] | |
| mbc = 0 | |
| mbd = 0 | |
| for _ in range(n): | |
| b, p, ft = input().split() | |
| b, p = (int(b), int(p)) | |
| f = (p, b) | |
| if ft == 'C': | |
| if p < tc: | |
| fc.append(f) | |
| mbc = max(mbc, b) | |
| elif p <= td: | |
| fd.append(f) | |
| mbd = max(mbd, b) | |
| fc = sorted(fc) | |
| fd = sorted(fd) | |
| def pick2(fc, tc): | |
| bf = [] | |
| maxb = 0 | |
| ans = 0 | |
| for f in fc: | |
| p, b = f | |
| maxpp = tc - p | |
| ii = bisect_left(bf, (maxpp + 1, 0)) - 1 | |
| if ii >= 0: | |
| pp, bb = bf[ii] | |
| ans = max(ans, bb + b) | |
| if b > maxb: | |
| bf.append(f) | |
| maxb = b | |
| return ans | |
| ans = mbc + mbd if mbc > 0 and mbd > 0 else 0 | |
| ans = max(ans, pick2(fc, tc)) | |
| ans = max(ans, pick2(fd, td)) | |
| print(ans) | |
| ``` | |
| ### 105 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1 - 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 106 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 4 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 107 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif not x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 108 | |
| ```python | |
| n, c, d = map(int, input().split()) | |
| inf = 10 ** 9 | |
| a = [0] * n | |
| sc = [] | |
| sd = [] | |
| for i in range(n): | |
| b, p, t = input().split() | |
| b = int(b) | |
| p = int(p) | |
| a[i] = [b, p, t] | |
| if t == 'C': | |
| sc.append([p, b]) | |
| else: | |
| sd.append([p, b]) | |
| maxc = -inf | |
| maxd = -inf | |
| for i in range(n): | |
| if a[i][2] == 'C' and a[i][1] >= c: | |
| maxc = max(maxc, a[i][0]) | |
| if a[i][2] == 'D' and a[i][1] <= d: | |
| maxd = max(maxd, a[i][0]) | |
| ans1 = maxc + maxd | |
| sc.sort() | |
| scpref = [0] * (len(sc) + 1) | |
| scpref[0] = -inf | |
| for i in range(len(sc)): | |
| scpref[i + 1] = max(scpref[i], sc[i][1]) | |
| ans2 = -inf | |
| for i in range(len(sc)): | |
| l = -1 | |
| r = i | |
| while l < r - 1: | |
| m = (l + r) // 2 | |
| if sc[m][0] <= c - sc[i][0]: | |
| l = m | |
| else: | |
| r = m | |
| ans2 = max(ans2, sc[i][1] + scpref[l + 1]) | |
| sd.sort() | |
| sdpref = [0] * (len(sd) + 1) | |
| sdpref[0] = -inf | |
| for i in range(len(sd)): | |
| sdpref[i + 1] = max(sdpref[i], sd[i][1]) | |
| ans3 = -inf | |
| for i in range(len(sd)): | |
| l = -1 | |
| r = i | |
| while l < r - 1: | |
| m = (l + r) // 2 | |
| if sd[m][0] <= d - sd[i][0]: | |
| l = m | |
| else: | |
| r = m | |
| ans3 = max(ans3, sd[i][1] + sdpref[l + 1]) | |
| ans = max(ans1, ans2, ans3) | |
| print(max(ans, 0)) | |
| ``` | |
| ### 109 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1 - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 110 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == -2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 111 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 5: | |
| ans *= 3 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 112 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1, n + 1): | |
| if testing[i] != None: | |
| continue | |
| score = 1 | |
| allscore = 1 | |
| testing[i] = 2 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 113 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-5) | |
| ``` | |
| ### 114 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = min(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result -= sums[key] - sums[parents[key]] | |
| else: | |
| result += sums[key] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 115 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if not pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 116 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X // Y | |
| print(ans) | |
| ``` | |
| ### 117 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(-4) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 118 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 119 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 3 or row_sum == 0 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 120 | |
| ```python | |
| import sys | |
| def read_data(): | |
| n = int(next(sys.stdin)) | |
| prices = list(map(int, next(sys.stdin).split())) | |
| vocabulary = [] | |
| for i in range(n): | |
| vocabulary.append(next(sys.stdin).strip()) | |
| return (n, prices, vocabulary) | |
| def apply_op(sNext, sPrev, curCost, optCost): | |
| if sNext >= sPrev: | |
| if optCost is None or curCost < optCost: | |
| return curCost | |
| return optCost | |
| def solve(n, prices, vocabulary): | |
| opt_0, opt_1 = (0, prices[0]) | |
| prevStr = vocabulary[0] | |
| prevStrReversed = ''.join(reversed(prevStr)) | |
| for curStr, curPrice in zip(vocabulary[1:], prices[1:]): | |
| curStrReversed = ''.join(reversed(curStr)) | |
| n_opt0, n_opt1 = (None, None) | |
| if opt_0 is not None: | |
| n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1) | |
| if opt_1 is not None: | |
| n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1) | |
| opt_0, opt_1 = (n_opt0, n_opt1) | |
| prevStr, prevStrReversed = (curStr, curStrReversed) | |
| if opt_0 is not None and opt_1 is None: | |
| break | |
| if opt_0 is None: | |
| return opt_1 | |
| if opt_1 is None: | |
| return opt_0 | |
| return min(opt_0, opt_1) | |
| def __starting_point(): | |
| n, prices, vocabulary = read_data() | |
| sorting_cost = solve(n, prices, vocabulary) | |
| if sorting_cost != None: | |
| print(sorting_cost) | |
| else: | |
| print(-1) | |
| __starting_point() | |
| ``` | |
| ### 121 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if not i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 122 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = max(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result += sums[key] - sums[parents[key]] | |
| else: | |
| result += sums[key] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 123 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[1] - q[0], reverse=True) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si * a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 0 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 124 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(1) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 125 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 2 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 126 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < 0: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = min([s[k] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i - 1])) | |
| print(ans) | |
| ``` | |
| ### 127 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 1) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 128 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K * d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 129 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry - 1] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 1: | |
| ans *= 3 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 130 | |
| ```python | |
| import sys | |
| def read_data(): | |
| n = int(next(sys.stdin)) | |
| prices = list(map(int, next(sys.stdin).split())) | |
| vocabulary = [] | |
| for i in range(n): | |
| vocabulary.append(next(sys.stdin).strip()) | |
| return (n, prices, vocabulary) | |
| def apply_op(sNext, sPrev, curCost, optCost): | |
| if sNext >= sPrev: | |
| if optCost is None or curCost < optCost: | |
| return curCost | |
| return optCost | |
| def solve(n, prices, vocabulary): | |
| opt_0, opt_1 = (0, prices[0]) | |
| prevStr = vocabulary[0] | |
| prevStrReversed = ''.join(reversed(prevStr)) | |
| for curStr, curPrice in zip(vocabulary[1:], prices[1:]): | |
| curStrReversed = ''.join(reversed(curStr)) | |
| n_opt0, n_opt1 = (None, None) | |
| if opt_0 is not None: | |
| n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1) | |
| if opt_1 is not None: | |
| n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1) | |
| opt_0, opt_1 = (n_opt0, n_opt1) | |
| prevStr, prevStrReversed = (curStr, curStrReversed) | |
| if opt_0 is None and opt_1 is None: | |
| break | |
| if opt_0 is None: | |
| return opt_1 | |
| if opt_1 is None: | |
| return opt_0 | |
| return min(opt_0, opt_1) | |
| def __starting_point(): | |
| n, prices, vocabulary = read_data() | |
| sorting_cost = solve(n, prices, vocabulary) | |
| if sorting_cost != None: | |
| print(sorting_cost) | |
| else: | |
| print(-0) | |
| __starting_point() | |
| ``` | |
| ### 131 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil(y * 1 / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 132 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < -1: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = min([s[k] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i])) | |
| print(ans) | |
| ``` | |
| ### 133 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 3: | |
| ans *= 3 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 134 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if not new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 135 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 4) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 136 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1 + 1, n + 1): | |
| if testing[i] != None: | |
| continue | |
| score = 1 | |
| allscore = 1 | |
| testing[i] = 1 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 137 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(--2) | |
| ``` | |
| ### 138 | |
| ```python | |
| n, t, d = map(int, input().split()) | |
| coin = [[(0, 0), (0, 0)] for i in range(100001)] | |
| diam = [[(0, 0), (0, 0)] for i in range(100001)] | |
| f = [] | |
| for i in range(n): | |
| b, c, q = input().split() | |
| b = int(b) | |
| c = int(c) | |
| if q == 'C': | |
| f.append((b, c, 1)) | |
| if coin[c][0][0] < b: | |
| coin[c][1] = coin[c][0] | |
| coin[c][0] = (b, i) | |
| elif coin[c][1][0] < b: | |
| coin[c][1] = (b, i) | |
| else: | |
| f.append((b, c, 2)) | |
| if diam[c][0][0] < b: | |
| diam[c][1] = diam[c][0] | |
| diam[c][0] = (b, i) | |
| elif diam[c][1][0] < b: | |
| diam[c][1] = (b, i) | |
| for i in range(2, 100001): | |
| if coin[i][0][0] < coin[i - 1][0][0]: | |
| coin[i][1] = max(coin[i][0], coin[i - 1][1]) | |
| coin[i][0] = coin[i - 1][0] | |
| else: | |
| coin[i][1] = max(coin[i - 1][1], coin[i][1]) | |
| if diam[i][0][0] < diam[i - 1][0][0]: | |
| diam[i][1] = max(diam[i][0], diam[i - 1][1]) | |
| diam[i][0] = diam[i - 1][0] | |
| else: | |
| diam[i][1] = max(diam[i - 1][1], diam[i][1]) | |
| p = False | |
| ans = 0 | |
| for i in range(n): | |
| fnt = f[i] | |
| if fnt[2] == 1: | |
| if t >= fnt[1]: | |
| s = t - fnt[1] | |
| if coin[s][0][0] > 0 and coin[s][0][1] != i: | |
| ans = max(fnt[0] + coin[s][0][0], ans) | |
| p = True | |
| elif coin[s][1][0] > 0: | |
| ans = max(fnt[0] + coin[s][1][0], ans) | |
| p = True | |
| if diam[d][0][0] > 0: | |
| ans = max(fnt[0] + diam[d][0][0], ans) | |
| p = True | |
| elif d >= fnt[1]: | |
| s = d - fnt[1] | |
| if diam[s][0][0] > 0 and diam[s][0][1] != i: | |
| ans = max(fnt[0] + diam[s][0][0], ans) | |
| p = True | |
| elif diam[s][1][0] > 0: | |
| ans = max(fnt[0] + diam[s][1][0], ans) | |
| p = True | |
| if coin[t][0][0] > 0: | |
| ans = max(fnt[0] + coin[t][0][0], ans) | |
| p = True | |
| if p: | |
| print(ans) | |
| else: | |
| print(1) | |
| ``` | |
| ### 139 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < 0: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = max([s[k] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i])) | |
| print(ans) | |
| ``` | |
| ### 140 | |
| ```python | |
| n = int(input()) | |
| c = [int(s) for s in input().split(' ')] | |
| C_rev = c[0] | |
| C_nrev = 0 | |
| last_s = input() | |
| last_s_rev = last_s[::-1] | |
| for i in range(1, n): | |
| s = input() | |
| s_rev = s[::-3] | |
| new_C_nrev = float('inf') | |
| if last_s <= s: | |
| new_C_nrev = min(new_C_nrev, C_nrev) | |
| if last_s_rev <= s: | |
| new_C_nrev = min(new_C_nrev, C_rev) | |
| new_C_rev = float('inf') | |
| if last_s <= s_rev: | |
| new_C_rev = min(new_C_rev, C_nrev + c[i]) | |
| if last_s_rev <= s_rev: | |
| new_C_rev = min(new_C_rev, C_rev + c[i]) | |
| if min(C_rev, C_nrev) == float('inf'): | |
| break | |
| C_rev = new_C_rev | |
| C_nrev = new_C_nrev | |
| last_s = s | |
| last_s_rev = s_rev | |
| print(-1 if min(C_rev, C_nrev) == float('inf') else min(C_rev, C_nrev)) | |
| ``` | |
| ### 141 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 0] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 142 | |
| ```python | |
| n = int(input()) | |
| c = [int(s) for s in input().split(' ')] | |
| C_rev = c[0] | |
| C_nrev = 0 | |
| last_s = input() | |
| last_s_rev = last_s[::-1] | |
| for i in reversed(range(1, n)): | |
| s = input() | |
| s_rev = s[::-1] | |
| new_C_nrev = float('inf') | |
| if last_s <= s: | |
| new_C_nrev = min(new_C_nrev, C_nrev) | |
| if last_s_rev <= s: | |
| new_C_nrev = min(new_C_nrev, C_rev) | |
| new_C_rev = float('inf') | |
| if last_s <= s_rev: | |
| new_C_rev = min(new_C_rev, C_nrev + c[i]) | |
| if last_s_rev <= s_rev: | |
| new_C_rev = min(new_C_rev, C_rev + c[i]) | |
| if min(C_rev, C_nrev) == float('inf'): | |
| break | |
| C_rev = new_C_rev | |
| C_nrev = new_C_nrev | |
| last_s = s | |
| last_s_rev = s_rev | |
| print(-1 if min(C_rev, C_nrev) == float('inf') else min(C_rev, C_nrev)) | |
| ``` | |
| ### 143 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(2): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 144 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = -1 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 145 | |
| ```python | |
| import math as m | |
| n = int(input()) | |
| cost = list(map(int, input().split())) | |
| s = [] | |
| sr = [] | |
| for i in range(n): | |
| s.append(list(input())) | |
| sr.append(list(reversed(s[i]))) | |
| dp = [[0, 0] for i in range(n + 1)] | |
| flag = 0 | |
| dp[0][1] = cost[1] | |
| for i in range(1, n): | |
| if dp[i - 1][0] == -1 and dp[i - 1][1] == -1: | |
| flag = 1 | |
| break | |
| elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1: | |
| if s[i] >= s[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1: | |
| if s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| else: | |
| if s[i] >= s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) | |
| elif s[i] >= s[i - 1] and s[i] < sr[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| elif s[i] < s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i] | |
| elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| if flag == 1: | |
| print(-1) | |
| else: | |
| res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1]) | |
| print(res) | |
| ``` | |
| ### 146 | |
| ```python | |
| import sys | |
| def get(v): | |
| if v == prev[v]: | |
| return v | |
| prev[v] = get(prev[v]) | |
| return prev[v] | |
| def join(u, v): | |
| u = get(u) | |
| v = get(v) | |
| if u != v: | |
| if size[u] < size[v]: | |
| u, v = (v, u) | |
| prev[v] = u | |
| size[u] += size[v] | |
| n, m = map(int, sys.stdin.readline().split()) | |
| edges = [0] * m | |
| for i in range(m): | |
| u, v, w = map(int, sys.stdin.readline().split()) | |
| edges[i] = (u - 1, v - 1, w) | |
| edges.sort(key=lambda e: e[-2]) | |
| prev = list(range(n)) | |
| size = [1] * n | |
| res = 0 | |
| l, r = (0, 1) | |
| while l < m: | |
| while r < m and edges[l][2] == edges[r][2]: | |
| r += 1 | |
| res += r - l | |
| for i in range(l, r): | |
| if get(edges[i][0]) == get(edges[i][1]): | |
| res -= 1 | |
| for i in range(l, r): | |
| if get(edges[i][0]) != get(edges[i][1]): | |
| join(edges[i][0], edges[i][1]) | |
| res -= 1 | |
| l = r | |
| print(res) | |
| ``` | |
| ### 147 | |
| ```python | |
| n, m = map(int, input().split()) | |
| class edge: | |
| def __init__(self, U, V, W): | |
| self.u = U | |
| self.v = V | |
| self.w = W | |
| l = [] | |
| for i in range(m): | |
| u, v, w = map(int, input().split()) | |
| l.append(edge(u, v, w)) | |
| l.sort(key=lambda x: x.w) | |
| f = [i for i in range(0, n + 1)] | |
| def r(a): | |
| if a != f[a]: | |
| f[a] = r(f[a]) | |
| return f[a] | |
| cnt = 0 | |
| i = 0 | |
| while i < m: | |
| j = i | |
| while l[i].w == l[j].w: | |
| j = j + 1 | |
| if j == m: | |
| break | |
| num = 0 | |
| for k in range(i, j): | |
| x = r(l[k].u) | |
| y = r(l[k].v) | |
| if x != y: | |
| num = num + 1 | |
| for k in range(i, j): | |
| x = r(l[k - 1].u) | |
| y = r(l[k].v) | |
| if x != y: | |
| f[y] = x | |
| num = num - 1 | |
| cnt = cnt + num | |
| i = j | |
| print(cnt) | |
| ``` | |
| ### 148 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and ((A_[1][1] and A_[2][1]) and (A_[3][1] and A_[6][1]) and (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][1] and A_[7][1]) or (A_[2][1] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][1] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 149 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 150 | |
| ```python | |
| n, t, d = map(int, input().split()) | |
| coin = [[(0, 0), (0, 0)] for i in range(100001)] | |
| diam = [[(0, 0), (0, 0)] for i in range(100001)] | |
| f = [] | |
| for i in range(n): | |
| b, c, q = input().split() | |
| b = int(b) | |
| c = int(c) | |
| if q == 'C': | |
| f.append((b, c, 1)) | |
| if coin[c][0][0] < b: | |
| coin[c][1] = coin[c][0] | |
| coin[c][0] = (b, i) | |
| elif coin[c][1][0] < b: | |
| coin[c][1] = (b, i) | |
| else: | |
| f.append((b, c, 2)) | |
| if diam[c][0][0] < b: | |
| diam[c][1] = diam[c][0] | |
| diam[c][0] = (b, i) | |
| elif diam[c][1][0] < b: | |
| diam[c][1] = (b, i) | |
| for i in range(2, 100001): | |
| if coin[i][0][0] < coin[i - 1][0][0]: | |
| coin[i][1] = max(coin[i][0], coin[i - 1][1]) | |
| coin[i][0] = coin[i - 1][0] | |
| else: | |
| coin[i][1] = max(coin[i - 1][1], coin[i][1]) | |
| if diam[i][0][0] < diam[i - 1][0][0]: | |
| diam[i][1] = max(diam[i][0], diam[i - 1][1]) | |
| diam[i][0] = diam[i - 1][0] | |
| else: | |
| diam[i][1] = max(diam[i - 1][1], diam[i][1]) | |
| p = False | |
| ans = 0 | |
| for i in range(n): | |
| fnt = f[i] | |
| if fnt[2] == 1: | |
| if t >= fnt[1]: | |
| s = t - fnt[1] | |
| if coin[s][0][0] > 0 and coin[s][0][1] != i: | |
| ans = max(fnt[0] + coin[s][0][0], ans) | |
| p = True | |
| elif coin[s][1][0] > 0: | |
| ans = max(fnt[0] + coin[s][1][0], ans) | |
| p = True | |
| if diam[d][0][0] > 0: | |
| ans = max(fnt[0] + diam[d][0][0], ans) | |
| p = True | |
| elif d >= fnt[1]: | |
| s = d - fnt[1] | |
| if diam[s][0][0] > 0 and diam[s][0][1] != i: | |
| ans = max(fnt[0] + diam[s][0][0], ans) | |
| p = True | |
| elif diam[s][1][0] > 0: | |
| ans = max(fnt[0] + diam[s][1][0], ans) | |
| p = True | |
| if coin[t][0][0] > 0: | |
| ans = max(fnt[0] + coin[t][0][0], ans) | |
| p = True | |
| if p: | |
| print(ans) | |
| else: | |
| print(4) | |
| ``` | |
| ### 151 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 4: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c + 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 2) | |
| print(res) | |
| ``` | |
| ### 152 | |
| ```python | |
| from operator import itemgetter | |
| def get_one_max(arr, resource): | |
| res = 0 | |
| for beauty, _ in [x for x in arr if x[1] >= resource]: | |
| res = max(res, beauty) | |
| return res | |
| def get_two_max(arr, resource): | |
| arr.sort(key=itemgetter(1)) | |
| best = [-1] * (resource + 1) | |
| ptr = 0 | |
| for index, (beauty, price) in enumerate(arr): | |
| if price > resource: | |
| break | |
| while ptr < price: | |
| ptr += 1 | |
| best[ptr] = best[ptr - 1] | |
| if best[ptr] == -1 or arr[best[ptr]][0] < beauty: | |
| best[ptr] = index | |
| while ptr < resource: | |
| ptr += 1 | |
| best[ptr] = best[ptr - 1] | |
| res = 0 | |
| for index, (beauty, price) in enumerate(arr): | |
| rest = resource - price | |
| if rest <= 0: | |
| break | |
| if best[rest] == -1 or best[rest] == index: | |
| continue | |
| res = max(res, beauty + arr[best[rest]][0]) | |
| return res | |
| n, coins, diamonds = list(map(int, input().split())) | |
| with_coins = [] | |
| with_diamonds = [] | |
| for i in range(n): | |
| beauty, price, tp = input().split() | |
| if tp == 'C': | |
| with_coins.append((int(beauty), int(price))) | |
| else: | |
| with_diamonds.append((int(beauty), int(price))) | |
| with_coins_max = get_one_max(with_coins, coins) | |
| with_diamonds_max = get_one_max(with_diamonds, diamonds) | |
| ans = 0 if with_coins_max == 0 or with_diamonds_max == 0 else with_coins_max + with_diamonds_max | |
| ans = max(ans, get_two_max(with_coins, coins)) | |
| ans = max(ans, get_two_max(with_diamonds, diamonds)) | |
| print(ans) | |
| ``` | |
| ### 153 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m >= n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(3, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 154 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2]) | |
| WCHANGE = [] | |
| for i in range(1, m): | |
| if EDGE[i - 1][2] != EDGE[i][1]: | |
| WCHANGE.append(i) | |
| WCHANGE.append(m) | |
| Group = [i for i in range(n + 1)] | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| NOW = 0 | |
| noneed = [0] * m | |
| ANS = 0 | |
| for wc in WCHANGE: | |
| for j in range(NOW, wc): | |
| if find(EDGE[j][0]) == find(EDGE[j][1]): | |
| noneed[j] = 1 | |
| for j in range(NOW, wc): | |
| if noneed[j] == 1: | |
| continue | |
| x, y, w = EDGE[j] | |
| if find(x) != find(y): | |
| Union(x, y) | |
| else: | |
| ANS += 1 | |
| NOW = wc | |
| print(ANS) | |
| ``` | |
| ### 155 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 0 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 2 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 156 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 4) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 157 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1 - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 158 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 1: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c - 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 2) | |
| print(res) | |
| ``` | |
| ### 159 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244357 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 160 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-0) | |
| ``` | |
| ### 161 | |
| ```python | |
| class DisjoinSet: | |
| def __init__(self, n): | |
| self.n = n | |
| self.dad = [i for i in range(n + 1)] | |
| self.cant = [1] * (n + 1) | |
| def SetOf(self, n): | |
| if self.dad[n] == n: | |
| return n | |
| self.dad[n] = self.SetOf(self.dad[n]) | |
| return self.dad[n] | |
| def Merge(self, a, b): | |
| a, b = (self.SetOf(a), self.SetOf(b)) | |
| if a != b: | |
| if self.cant[b] > self.cant[a]: | |
| a, b = (b, a) | |
| self.cant[a] += self.cant[b] | |
| self.dad[b] = a | |
| return True | |
| return False | |
| def Kruskal(l, n): | |
| l.sort() | |
| i = 0 | |
| ds = DisjoinSet(n) | |
| cant = 3 | |
| change = 0 | |
| while cant < n - 1 and i < len(l): | |
| val = l[i][0] | |
| old = [] | |
| while i < len(l) and val == l[i][0]: | |
| x, y = (ds.SetOf(l[i][1]), ds.SetOf(l[i][2])) | |
| if x != y: | |
| old.append((x, y)) | |
| i += 1 | |
| change += len(old) | |
| for x, y in old: | |
| change -= ds.Merge(x, y) | |
| return change | |
| n, m = input().split() | |
| n, m = (int(n), int(m)) | |
| e = [] | |
| for i in range(m): | |
| x, y, c = input().split() | |
| e.append((int(c), int(x), int(y))) | |
| print(Kruskal(e, n)) | |
| ``` | |
| ### 162 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 163 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244355 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 164 | |
| ```python | |
| import sys | |
| def read_data(): | |
| n = int(next(sys.stdin)) | |
| prices = list(map(int, next(sys.stdin).split())) | |
| vocabulary = [] | |
| for i in range(n): | |
| vocabulary.append(next(sys.stdin).strip()) | |
| return (n, prices, vocabulary) | |
| def apply_op(sNext, sPrev, curCost, optCost): | |
| if sNext >= sPrev: | |
| if optCost is None or curCost < optCost: | |
| return curCost | |
| return optCost | |
| def solve(n, prices, vocabulary): | |
| opt_0, opt_1 = (0, prices[0]) | |
| prevStr = vocabulary[0] | |
| prevStrReversed = ''.join(reversed(prevStr)) | |
| for curStr, curPrice in zip(vocabulary[1:], prices[1:]): | |
| curStrReversed = ''.join(reversed(curStr)) | |
| n_opt0, n_opt1 = (None, None) | |
| if opt_0 is not None: | |
| n_opt0 = apply_op(curStr, prevStr, opt_0, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStr, opt_0 + curPrice, n_opt1) | |
| if opt_1 is not None: | |
| n_opt0 = apply_op(curStr, prevStrReversed, opt_1, n_opt0) | |
| n_opt1 = apply_op(curStrReversed, prevStrReversed, opt_1 + curPrice, n_opt1) | |
| opt_0, opt_1 = (n_opt0, n_opt1) | |
| prevStr, prevStrReversed = (curStr, curStrReversed) | |
| if opt_0 is None and opt_1 is not None: | |
| break | |
| if opt_0 is None: | |
| return opt_1 | |
| if opt_1 is None: | |
| return opt_0 | |
| return min(opt_0, opt_1) | |
| def __starting_point(): | |
| n, prices, vocabulary = read_data() | |
| sorting_cost = solve(n, prices, vocabulary) | |
| if sorting_cost != None: | |
| print(sorting_cost) | |
| else: | |
| print(-1) | |
| __starting_point() | |
| ``` | |
| ### 165 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 1: | |
| ans *= 1 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 166 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-2) | |
| ``` | |
| ### 167 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(-1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 168 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if n * y % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 169 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 0 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 4 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 170 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 1 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 171 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 2 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 172 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if y - 1 & d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 173 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1 - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < 0: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = min([s[k] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i])) | |
| print(ans) | |
| ``` | |
| ### 174 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = min(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result += sums[key - 1] - sums[parents[key]] | |
| else: | |
| result += sums[key] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 175 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1 - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 176 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][1] and A_[7][1]) or (A_[2][0] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][1] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 177 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = -2 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 178 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[-3] - q[0]) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si * a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 0 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 179 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = 3 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 180 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == -1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 181 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-2) | |
| ``` | |
| ### 182 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1 - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 183 | |
| ```python | |
| n, m = map(int, input().split()) | |
| class edge: | |
| def __init__(self, U, V, W): | |
| self.u = U | |
| self.v = V | |
| self.w = W | |
| l = [] | |
| for i in range(m): | |
| u, v, w = map(int, input().split()) | |
| l.append(edge(u, v, w)) | |
| l.sort(key=lambda x: x.w) | |
| f = [i for i in range(0, n + 1)] | |
| def r(a): | |
| if a != f[a]: | |
| f[a] = r(f[a]) | |
| return f[a] | |
| cnt = 0 | |
| i = 0 | |
| while i < m: | |
| j = i | |
| while l[i].w == l[j].w: | |
| j = j + 1 | |
| if j == m: | |
| break | |
| num = 0 | |
| for k in range(i, j): | |
| x = r(l[k - 1].u) | |
| y = r(l[k].v) | |
| if x != y: | |
| num = num + 1 | |
| for k in range(i, j): | |
| x = r(l[k].u) | |
| y = r(l[k].v) | |
| if x != y: | |
| f[y] = x | |
| num = num - 1 | |
| cnt = cnt + num | |
| i = j | |
| print(cnt) | |
| ``` | |
| ### 184 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2]) | |
| WCHANGE = [] | |
| for i in range(1, m): | |
| if EDGE[i - 1][2] != EDGE[i][0]: | |
| WCHANGE.append(i) | |
| WCHANGE.append(m) | |
| Group = [i for i in range(n + 1)] | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| NOW = 0 | |
| noneed = [0] * m | |
| ANS = 0 | |
| for wc in WCHANGE: | |
| for j in range(NOW, wc): | |
| if find(EDGE[j][0]) == find(EDGE[j][1]): | |
| noneed[j] = 1 | |
| for j in range(NOW, wc): | |
| if noneed[j] == 1: | |
| continue | |
| x, y, w = EDGE[j] | |
| if find(x) != find(y): | |
| Union(x, y) | |
| else: | |
| ANS += 1 | |
| NOW = wc | |
| print(ANS) | |
| ``` | |
| ### 185 | |
| ```python | |
| import sys | |
| line = sys.stdin.readlines() | |
| n = int(line[0]) | |
| c = list(map(int, line[1].split())) | |
| no = (0, '') | |
| yes = (0, '') | |
| for i in range(n): | |
| s = line[i + 2].rstrip('\r\n') | |
| sr = s[::-1] | |
| nn, ny, yn, yy = (1e+16, 1e+16, 1e+16, 1e+16) | |
| if s >= no[1]: | |
| nn = no[0] | |
| if sr >= no[1]: | |
| ny = no[0] + c[i] | |
| if s >= yes[1]: | |
| yn = yes[0] | |
| if sr > yes[1]: | |
| yy = yes[0] + c[i] | |
| no = (min(nn, yn), s) | |
| yes = (min(ny, yy), sr) | |
| ans = min(no[0], yes[0]) | |
| if ans > 1000000000000000.0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 186 | |
| ```python | |
| import math as m | |
| n = int(input()) | |
| cost = list(map(int, input().split())) | |
| s = [] | |
| sr = [] | |
| for i in range(n): | |
| s.append(list(input())) | |
| sr.append(list(reversed(s[i]))) | |
| dp = [[3, 0] for i in range(n + 1)] | |
| flag = 0 | |
| dp[0][1] = cost[0] | |
| for i in range(1, n): | |
| if dp[i - 1][0] == -1 and dp[i - 1][1] == -1: | |
| flag = 1 | |
| break | |
| elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1: | |
| if s[i] >= s[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1: | |
| if s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| else: | |
| if s[i] >= s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) | |
| elif s[i] >= s[i - 1] and s[i] < sr[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| elif s[i] < s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i] | |
| elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| if flag == 1: | |
| print(-1) | |
| else: | |
| res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1]) | |
| print(res) | |
| ``` | |
| ### 187 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = min(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result += sums[key] - sums[parents[key]] | |
| else: | |
| result += sums[key + 1] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 188 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-1) | |
| ``` | |
| ### 189 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2]) | |
| WCHANGE = [] | |
| for i in range(1, m): | |
| if EDGE[i - 1][2] != EDGE[i][2]: | |
| WCHANGE.append(i) | |
| WCHANGE.append(m) | |
| Group = [i for i in range(n + 1)] | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| NOW = 0 | |
| noneed = [0] * m | |
| ANS = 0 | |
| for wc in WCHANGE: | |
| for j in range(NOW, wc - 1): | |
| if find(EDGE[j][0]) == find(EDGE[j][1]): | |
| noneed[j] = 1 | |
| for j in range(NOW, wc): | |
| if noneed[j] == 1: | |
| continue | |
| x, y, w = EDGE[j] | |
| if find(x) != find(y): | |
| Union(x, y) | |
| else: | |
| ANS += 1 | |
| NOW = wc | |
| print(ANS) | |
| ``` | |
| ### 190 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244353 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(0, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 191 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S - tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 0: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h > 2: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 192 | |
| ```python | |
| n, m = map(int, input().split()) | |
| class edge: | |
| def __init__(self, U, V, W): | |
| self.u = U | |
| self.v = V | |
| self.w = W | |
| l = [] | |
| for i in range(m): | |
| u, v, w = map(int, input().split()) | |
| l.append(edge(u, v, w)) | |
| l.sort(key=lambda x: x.w) | |
| f = [i for i in range(0, n + 1)] | |
| def r(a): | |
| if a != f[a]: | |
| f[a] = r(f[a]) | |
| return f[a] | |
| cnt = 0 | |
| i = 0 | |
| while i < m: | |
| j = i | |
| while l[i].w == l[j].w: | |
| j = j + 1 | |
| if j == m: | |
| break | |
| num = 0 | |
| for k in range(i, j): | |
| x = r(l[k].u) | |
| y = r(l[k].v) | |
| if x != y: | |
| num = num + 1 | |
| for k in range(i, j): | |
| x = r(l[k].u) | |
| y = r(l[k - 1].v) | |
| if x != y: | |
| f[y] = x | |
| num = num - 1 | |
| cnt = cnt + num | |
| i = j | |
| print(cnt) | |
| ``` | |
| ### 193 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-5) | |
| ``` | |
| ### 194 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil(n * y / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 195 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 0 or (col_sum == 3): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 196 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo * m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 197 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S - tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 2: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h > 0: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 198 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2]) | |
| WCHANGE = [] | |
| for i in range(1, m): | |
| if EDGE[i - 1][2] != EDGE[i][2]: | |
| WCHANGE.append(i) | |
| WCHANGE.append(m) | |
| Group = [i for i in range(n + 1)] | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| NOW = 0 | |
| noneed = [0] * m | |
| ANS = 0 | |
| for wc in WCHANGE: | |
| for j in range(NOW, wc): | |
| if find(EDGE[j][0]) == find(EDGE[j][1]): | |
| noneed[j] = 1 | |
| for j in range(NOW, wc): | |
| if noneed[j - 1] == 1: | |
| continue | |
| x, y, w = EDGE[j] | |
| if find(x) != find(y): | |
| Union(x, y) | |
| else: | |
| ANS += 1 | |
| NOW = wc | |
| print(ANS) | |
| ``` | |
| ### 199 | |
| ```python | |
| import math as m | |
| n = int(input()) | |
| cost = list(map(int, input().split())) | |
| s = [] | |
| sr = [] | |
| for i in range(n): | |
| s.append(list(input())) | |
| sr.append(list(reversed(s[i]))) | |
| dp = [[0, 0] for i in range(n + 1)] | |
| flag = 0 | |
| dp[0][1] = cost[0] | |
| for i in range(1, n): | |
| if dp[i - 1][0] == -0 and dp[i - 1][1] == -1: | |
| flag = 1 | |
| break | |
| elif dp[i - 1][0] != -1 and dp[i - 1][1] == -1: | |
| if s[i] >= s[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| elif dp[i - 1][0] == -1 and dp[i - 1][1] != -1: | |
| if s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| else: | |
| if s[i] >= s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) | |
| elif s[i] >= s[i - 1] and s[i] < sr[i - 1]: | |
| dp[i][0] = dp[i - 1][0] | |
| elif s[i] < s[i - 1] and s[i] >= sr[i - 1]: | |
| dp[i][0] = dp[i - 1][1] | |
| else: | |
| dp[i][0] = -1 | |
| if sr[i] >= s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i] | |
| elif sr[i] >= s[i - 1] and sr[i] < sr[i - 1]: | |
| dp[i][1] = dp[i - 1][0] + cost[i] | |
| elif sr[i] < s[i - 1] and sr[i] >= sr[i - 1]: | |
| dp[i][1] = dp[i - 1][1] + cost[i] | |
| else: | |
| dp[i][1] = -1 | |
| if flag == 1: | |
| print(-1) | |
| else: | |
| res = max(dp[n - 1][0], dp[n - 1][1]) if min(dp[n - 1][0], dp[n - 1][1]) == -1 else min(dp[n - 1][0], dp[n - 1][1]) | |
| print(res) | |
| ``` | |
| ### 200 | |
| ```python | |
| n, c, d = map(int, input().split()) | |
| inf = 10 ** 9 | |
| a = [0] * n | |
| sc = [] | |
| sd = [] | |
| for i in range(n): | |
| b, p, t = input().split() | |
| b = int(b) | |
| p = int(p) | |
| a[i] = [b, p, t] | |
| if t == 'C': | |
| sc.append([p, b]) | |
| else: | |
| sd.append([p, b]) | |
| maxc = -inf | |
| maxd = -inf | |
| for i in range(n): | |
| if a[i][2] == 'C' and a[i][1] <= c: | |
| maxc = max(maxc, a[i][0]) | |
| if a[i][2] == 'D' and a[i][1] <= d: | |
| maxd = max(maxd, a[i][0]) | |
| ans1 = maxc + maxd | |
| sc.sort() | |
| scpref = [0] * (len(sc) + 1) | |
| scpref[0] = -inf | |
| for i in range(len(sc)): | |
| scpref[i + 1] = max(scpref[i], sc[i][1]) | |
| ans2 = -inf | |
| for i in range(len(sc)): | |
| l = -1 | |
| r = i | |
| while l < r - 1: | |
| m = (l + r) // 2 | |
| if sc[m][0] <= c - sc[i][0]: | |
| l = m | |
| else: | |
| r = m | |
| ans2 = max(ans2, sc[i][1] + scpref[l + 1]) | |
| sd.sort(reverse=True) | |
| sdpref = [0] * (len(sd) + 1) | |
| sdpref[0] = -inf | |
| for i in range(len(sd)): | |
| sdpref[i + 1] = max(sdpref[i], sd[i][1]) | |
| ans3 = -inf | |
| for i in range(len(sd)): | |
| l = -1 | |
| r = i | |
| while l < r - 1: | |
| m = (l + r) // 2 | |
| if sd[m][0] <= d - sd[i][0]: | |
| l = m | |
| else: | |
| r = m | |
| ans3 = max(ans3, sd[i][1] + sdpref[l + 1]) | |
| ans = max(ans1, ans2, ans3) | |
| print(max(ans, 0)) | |
| ``` | |
| ### Final Query | |
| Identify every unordered pair of earlier programs that implements the same input-output specification, including its boundary and edge-case behavior. Each cell may appear in at most one returned pair. Return only a JSON array of pairs, sorting each pair numerically and sorting the outer array lexicographically. Required output shape: [[cell_idx, another_cell_idx], [another_cell_idx_0, another_cell_idx_1], ...]. Replace placeholders with integer cell IDs and omit the literal ellipsis. | |