|
| 1 | +import sys |
| 2 | +from collections import defaultdict, deque |
| 3 | + |
| 4 | +read = lambda: sys.stdin.readline().rstrip() |
| 5 | + |
| 6 | + |
| 7 | +class Problem: |
| 8 | + def __init__(self) -> None: |
| 9 | + self.n, self.m = map(int, read().split()) |
| 10 | + |
| 11 | + self.graph = defaultdict(list) |
| 12 | + for x, y in [map(int, read().split()) for _ in range(self.m)]: |
| 13 | + self.graph[self.literal_to_node(-x)].append(self.literal_to_node(y)) |
| 14 | + self.graph[self.literal_to_node(-y)].append(self.literal_to_node(x)) |
| 15 | + |
| 16 | + def literal_to_node(self, literal: int) -> int: |
| 17 | + if literal > 0: |
| 18 | + return literal |
| 19 | + |
| 20 | + return -literal + self.n |
| 21 | + |
| 22 | + def solve(self) -> None: |
| 23 | + discovered, low, scc_id, on_stack, scc_stack, order, scc_counter = ( |
| 24 | + [-1] * (2 * self.n + 1), |
| 25 | + [-1] * (2 * self.n + 1), |
| 26 | + [-1] * (2 * self.n + 1), |
| 27 | + [False] * (2 * self.n + 1), |
| 28 | + deque(), |
| 29 | + 0, |
| 30 | + 0, |
| 31 | + ) |
| 32 | + |
| 33 | + for node in range(1, 2 * self.n + 1): |
| 34 | + if discovered[node] == -1: |
| 35 | + order, scc_counter = self.tarjan_scc( |
| 36 | + node, |
| 37 | + discovered, |
| 38 | + low, |
| 39 | + scc_id, |
| 40 | + on_stack, |
| 41 | + scc_stack, |
| 42 | + order, |
| 43 | + scc_counter, |
| 44 | + ) |
| 45 | + |
| 46 | + for idx in range(1, self.n + 1): |
| 47 | + if scc_id[idx] == scc_id[idx + self.n]: |
| 48 | + print(0) |
| 49 | + return |
| 50 | + |
| 51 | + print(1) |
| 52 | + print(*[0 if scc_id[idx] > scc_id[idx + self.n] else 1 for idx in range(1, self.n + 1)]) |
| 53 | + |
| 54 | + def tarjan_scc( |
| 55 | + self, |
| 56 | + start_node: int, |
| 57 | + discovered: list[int], |
| 58 | + low: list[int], |
| 59 | + scc_id: list[int], |
| 60 | + on_stack: list[bool], |
| 61 | + scc_stack: deque, |
| 62 | + order: int, |
| 63 | + scc_counter: int, |
| 64 | + ) -> tuple[int, int]: |
| 65 | + stack = deque([(start_node, iter(self.graph[start_node]))]) |
| 66 | + |
| 67 | + while stack: |
| 68 | + current_node, neighbors = stack[-1] |
| 69 | + |
| 70 | + if discovered[current_node] == -1: |
| 71 | + order += 1 |
| 72 | + discovered[current_node] = low[current_node] = order |
| 73 | + scc_stack.append(current_node) |
| 74 | + on_stack[current_node] = True |
| 75 | + |
| 76 | + for neighbor in neighbors: |
| 77 | + if discovered[neighbor] == -1: |
| 78 | + stack.append((neighbor, iter(self.graph[neighbor]))) |
| 79 | + break |
| 80 | + elif on_stack[neighbor]: |
| 81 | + low[current_node] = min(low[current_node], discovered[neighbor]) |
| 82 | + else: |
| 83 | + popped_node, _ = stack.pop() |
| 84 | + |
| 85 | + if low[popped_node] == discovered[popped_node]: |
| 86 | + scc_counter += 1 |
| 87 | + while True: |
| 88 | + node_in_scc = scc_stack.pop() |
| 89 | + on_stack[node_in_scc] = False |
| 90 | + scc_id[node_in_scc] = scc_counter |
| 91 | + if node_in_scc == popped_node: |
| 92 | + break |
| 93 | + |
| 94 | + if stack: |
| 95 | + parent_node, _ = stack[-1] |
| 96 | + low[parent_node] = min(low[parent_node], low[popped_node]) |
| 97 | + |
| 98 | + return order, scc_counter |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + Problem().solve() |
0 commit comments