- 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
- 75 lines of Python from the credited upstream file abc282_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 7 input = sys.stdin.readline8 9 n, m = map(int, input().split())10 graph = [[] for _ in range(n)]11 12 for _ in range(m):13 ai, bi = map(int, input().split())14 ai -= 115 bi -= 116 17 graph[ai].append(bi)18 graph[bi].append(ai)19 20 21 22 no_color, black, white = 0, 1, -123 colors = [no_color] * n 24 25 def is_bipartite(i, black_count = 0, white_count = 0):26 stack = [(i, black)] 27 28 while stack:29 vertex, color = stack.pop()30 31 if colors[vertex] != no_color:32 continue33 34 colors[vertex] = color35 36 if color == black:37 black_count += 138 else:39 white_count += 140 41 for to in graph[vertex]:42 if colors[to] == color:43 return False, 0, 044 45 if colors[to] == no_color:46 stack.append((to, -color)) 47 48 return True, black_count, white_count49 50 51 def nC2(n):52 return n * (n - 1) 253 54 ans = nC2(n) - m55 56 57 for i in range(n):58 if colors[i] != no_color:59 continue60 61 flag, black_count, white_count = is_bipartite(i)62 63 if flag:64 ans -= nC2(black_count)65 ans -= nC2(white_count)66 else:67 print(0)68 exit()69 70 print(ans)71 72 73if __name__ == "__main__":74 main()75