-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm.py
More file actions
209 lines (144 loc) · 5.57 KB
/
m.py
File metadata and controls
209 lines (144 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from __future__ import annotations
import enum
import itertools
from collections import deque
from collections.abc import Iterable, Iterator
from typing import Self
class Edge:
vertices: tuple[int, int]
def __init__(self, vertices: Iterable[int]) -> None:
vertices_list = list(itertools.islice(vertices, 2))
self.vertices = vertices_list[0], vertices_list[1]
def __iter__(self) -> Iterator[int]:
yield from self.vertices
def __getitem__(self, index: int) -> int:
return self.vertices[index]
def __neg__(self) -> Self:
return self.__class__([self[1], self[0]])
@classmethod
def read(cls) -> Self:
vertices_iter = map(lambda vertex_str: int(vertex_str) - 1, input().split())
return cls(vertices_iter)
@classmethod
def read_list(cls, count: int) -> Iterable[Self]:
for i in range(count):
yield cls.read()
class AdjacencyList:
vertices: list[int]
def __init__(self) -> None:
self.vertices = []
def __len__(self) -> int:
return len(self.vertices)
def __iter__(self) -> Iterator[int]:
yield from self.vertices
def add_vertex(self, vertex: int) -> None:
self.vertices.append(vertex)
class Graph:
is_directed: bool
adjacency_lists: list[AdjacencyList]
def __init__(self, *, vertices_count: int = 0, is_directed: bool = True) -> None:
self.is_directed = is_directed
self.adjacency_lists = []
self.create_vertices(vertices_count)
def __len__(self) -> int:
return len(self.adjacency_lists)
def __iter__(self) -> Iterator[AdjacencyList]:
yield from self.adjacency_lists
def __getitem__(self, vertex: int) -> AdjacencyList:
return self.adjacency_lists[vertex]
def create_vertices(self, count: int) -> None:
self.adjacency_lists.extend(AdjacencyList() for _i in range(count))
def add_edge(self, edge: Edge) -> None:
self._add_edge(edge)
if not self.is_directed:
self._add_edge(-edge)
def _add_edge(self, edge: Edge) -> None:
adjacency_list = self.adjacency_lists[edge[0]]
adjacency_list.add_vertex(edge[1])
def is_bipartite(self) -> bool:
bipartite_checker = BipartiteChecker(self)
return bipartite_checker.check()
@classmethod
def read(cls, *, vertices_count: int, edges_count: int, is_directed: bool = True) -> Self:
graph = cls(vertices_count=vertices_count, is_directed=is_directed)
for edge in Edge.read_list(edges_count):
graph.add_edge(edge)
return graph
class VertexColor(enum.Enum):
WHITE = 0
BLUE = 1
RED = 2
@classmethod
def get_first(cls) -> VertexColor:
return cls.BLUE
def get_next(self) -> VertexColor:
if self is self.WHITE:
return self.get_first()
return VertexColor.RED if self is VertexColor.BLUE else VertexColor.BLUE
class VerticesState:
colors: list[VertexColor]
def __init__(self, *, vertices_count: int = 0) -> None:
self.colors = [VertexColor.WHITE] * vertices_count
def is_visited(self, vertex: int) -> bool:
return self.colors[vertex] is not VertexColor.WHITE
def visit(self, vertex: int, *, previous_vertex: int | None = None) -> None:
self.colors[vertex] = (
VertexColor.get_first()
if previous_vertex is None
else self.colors[previous_vertex].get_next()
)
def same_partition(self, first_vertex: int, second_vertex: int) -> bool:
if not (self.is_visited(first_vertex) and self.is_visited(second_vertex)):
return False
return self.colors[first_vertex] is self.colors[second_vertex]
class VerticesQueue:
vertices: deque[int]
def __init__(self) -> None:
self.vertices = deque()
def __bool__(self) -> bool:
return bool(self.vertices)
def put(self, vertex: int) -> None:
self.vertices.append(vertex)
def get(self) -> int:
return self.vertices.popleft()
class BipartiteChecker:
graph: Graph
state: VerticesState
queue: VerticesQueue
def __init__(self, graph: Graph) -> None:
self.graph = graph
self.state = VerticesState()
self.queue = VerticesQueue()
def check(self) -> bool:
vertices_count = len(self.graph)
self.state = VerticesState(vertices_count=vertices_count)
self.queue = VerticesQueue()
for start_vertex in range(vertices_count):
if self.state.is_visited(start_vertex):
continue
if not self._check_component(start_vertex):
return False
return True
def _check_component(self, start_vertex: int) -> bool:
self._visit_vertex(start_vertex)
while self.queue:
vertex = self.queue.get()
for neighbor in self.graph[vertex]:
if not self.state.is_visited(neighbor):
self._visit_vertex(neighbor, previous_vertex=vertex)
elif self.state.same_partition(vertex, neighbor):
return False
return True
def _visit_vertex(self, vertex: int, *, previous_vertex: int | None = None) -> None:
self.state.visit(vertex, previous_vertex=previous_vertex)
self.queue.put(vertex)
def main() -> None:
vertices_count, edges_count = map(int, input().split())
graph = Graph.read(
vertices_count=vertices_count,
edges_count=edges_count,
is_directed=False,
)
print('YES' if graph.is_bipartite() else 'NO')
if __name__ == '__main__':
main()