We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8ece4c8 commit 7e3d396Copy full SHA for 7e3d396
dfs-bfs.md
@@ -334,6 +334,30 @@ def bfs(start):
334
q.enqueue(u)
335
```
336
337
+Так как мы заходим в `if` для каждой вершины лишь один раз, то метку с порядком посещения можно выставить и внутри него:
338
+
339
+```python
340
+next_idx = 0
341
342
+def bfs(start):
343
+ q = queue()
344
345
+ visited[start] = True
346
+ q.enqueue(start)
347
348
+ while not q.empty():
349
+ v = q.dequeue()
350
351
+ for u in g[v]:
352
+ if not visited[u]:
353
+ visited[u] = True
354
355
+ ord[v] = next_idx # <-- вот
356
+ next_idx += 1 # <-- здесь!
357
358
+ q.enqueue(u)
359
+```
360
361
Если хотим найти растояние в ребрах до всех вершин:
362
363
```python
0 commit comments