- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 76 lines of Python from the credited upstream file abc327_d.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
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 typing import List, Tuple7 8 input = sys.stdin.readline9 10 n, m = map(int, input().split())11 a = list(map(int, input().split()))12 b = list(map(int, input().split()))13 graph = [[] for _ in range(n)]14 no_color, black, white = 0, 1, -115 colors = [no_color] * n16 17 for ai, bi in zip(a, b):18 ai -= 119 bi -= 120 21 graph[ai].append(bi)22 graph[bi].append(ai)23 24 25 26 def is_bipartite(27 start_id: int,28 graph: List[List],29 colors: List[int],30 black_count: int = 0,31 white_count: int = 0,32 ) -> Tuple[bool, int, int]:33 stack = [(start_id, black)] 34 35 while stack:36 vertex, color = stack.pop()37 38 if colors[vertex] != no_color:39 continue40 41 colors[vertex] = color42 43 if color == black:44 black_count += 145 elif color == white:46 white_count += 147 48 for to in graph[vertex]:49 if colors[to] == color:50 return False, 0, 051 52 if colors[to] == no_color:53 stack.append((to, -color)) 54 55 return True, black_count, white_count56 57 for i in range(n):58 if colors[i] != no_color:59 continue60 61 flag, black_count, white_count = is_bipartite(62 start_id=i, graph=graph, colors=colors63 )64 65 66 67 if not flag:68 print("No")69 exit()70 71 print("Yes")72 73 74if __name__ == "__main__":75 main()76