- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 65 lines of Python from the credited upstream file abc320_d.py.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup, work queue.
- No explicit loop blocks detected.
Complexity
Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4def main():5 import sys6 from collections import defaultdict, deque7 from typing import List, Tuple8 9 input = sys.stdin.readline10 11 n, m = map(int, input().split())12 graph = [[] for _ in range(n)]13 14 for _ in range(m):15 ai, bi, xi, yi = map(int, input().split())16 ai -= 117 bi -= 118 19 graph[ai].append((bi, xi, yi))20 graph[bi].append((ai, -xi, -yi)) 21 22 inf = 10**1823 24 def bfs_for_graph(vertex_count: int, graph: List[List[int]], start_id: int):25 d = deque()26 d.append((start_id, 0, 0))27 visited = [False] * vertex_count28 dist = defaultdict(tuple)29 dist[0] = (0, 0)30 31 while d:32 cur, xi, yi = d.pop()33 34 if visited[cur]:35 continue36 37 visited[cur] = True38 39 for to, xj, yj in graph[cur]:40 nx = xi + xj41 ny = yi + yj42 43 if dist[to] != tuple():44 continue45 46 dist[to] = (nx, ny)47 d.append((to, nx, ny))48 49 return visited, dist50 51 start_id = 052 visited, dist = bfs_for_graph(vertex_count=n, graph=graph, start_id=start_id)53 54 for i in range(n):55 if dist[i] == tuple():56 print("undecidable")57 continue58 59 xi, yi = dist[i]60 print(xi, yi)61 62 63if __name__ == "__main__":64 main()65