From 8eea04d62cbbfc8090100ddb854e8414919759c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=92=D0=B0=D1=81?= =?UTF-8?q?=D0=B8=D0=BB=D1=8C=D0=B5=D0=B2?= Date: Mon, 27 Apr 2026 18:54:37 +0300 Subject: [PATCH 1/2] feat: 1466. Reorder Routes to Make All Paths Lead to the City Zero --- .../__init__.py | 0 .../solution.py | 24 +++++++++++++++++++ ...to_make_all_paths_lead_to_the_city_zero.py | 17 +++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 src/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero/__init__.py create mode 100644 src/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero/solution.py create mode 100644 tests/test_reorder_routes_to_make_all_paths_lead_to_the_city_zero.py diff --git a/src/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero/__init__.py b/src/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero/solution.py b/src/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero/solution.py new file mode 100644 index 0000000..cb5e01c --- /dev/null +++ b/src/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero/solution.py @@ -0,0 +1,24 @@ +class Solution: + def dfs( + self, + adjacent: list[list[tuple[int, int]]], + visited: list[bool], + current_city: int, + ) -> int: + visited[current_city] = True + changes = 0 + for neighbour, direction in adjacent[current_city]: + if not visited[neighbour]: + if direction == 1: + changes += 1 + changes += self.dfs(adjacent, visited, neighbour) + + return changes + + def minReorder(self, n: int, connections: list[list[int]]) -> int: + adjacent = [[] for _ in range(n)] + for u, v in connections: + adjacent[u].append((v, 1)) + adjacent[v].append((u, -1)) + visited = [False] * n + return self.dfs(adjacent, visited, 0) diff --git a/tests/test_reorder_routes_to_make_all_paths_lead_to_the_city_zero.py b/tests/test_reorder_routes_to_make_all_paths_lead_to_the_city_zero.py new file mode 100644 index 0000000..1444955 --- /dev/null +++ b/tests/test_reorder_routes_to_make_all_paths_lead_to_the_city_zero.py @@ -0,0 +1,17 @@ +import pytest +from src.graphs.reorder_routes_to_make_all_paths_lead_to_the_city_zero.solution import ( + Solution, +) + + +@pytest.mark.parametrize( + "n, connecntions, expected", + [ + (6, [[0, 1], [1, 3], [2, 3], [4, 0], [4, 5]], 3), + (5, [[1, 0], [1, 2], [3, 2], [3, 4]], 2), + (3, [[1, 0], [2, 0]], 0), + ], +) +def test_min_reorder(n, connections, expected): + solution = Solution() + assert solution.minReorder(n, connections) == expected From 395836f821f8487712c8d5c7019f4caa2f371630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=92=D0=B0=D1=81?= =?UTF-8?q?=D0=B8=D0=BB=D1=8C=D0=B5=D0=B2?= Date: Mon, 27 Apr 2026 18:58:39 +0300 Subject: [PATCH 2/2] fix: test --- ...st_reorder_routes_to_make_all_paths_lead_to_the_city_zero.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_reorder_routes_to_make_all_paths_lead_to_the_city_zero.py b/tests/test_reorder_routes_to_make_all_paths_lead_to_the_city_zero.py index 1444955..126ebb7 100644 --- a/tests/test_reorder_routes_to_make_all_paths_lead_to_the_city_zero.py +++ b/tests/test_reorder_routes_to_make_all_paths_lead_to_the_city_zero.py @@ -5,7 +5,7 @@ @pytest.mark.parametrize( - "n, connecntions, expected", + "n, connections, expected", [ (6, [[0, 1], [1, 3], [2, 3], [4, 0], [4, 5]], 3), (5, [[1, 0], [1, 2], [3, 2], [3, 4]], 2),