-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
68 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+156 KB
images/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 重新规划路线 | ||
// https://leetcode.cn/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero | ||
// INLINE ../../images/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero.jpeg | ||
|
||
pub struct Solution; | ||
|
||
impl Solution { | ||
fn dfs(node: i32, visited: &mut Vec<bool>, res: &mut i32, graph: &Vec<Vec<(i32, bool)>>) { | ||
visited[node as usize] = true; | ||
for (next, is_out) in &graph[node as usize] { | ||
if !visited[*next as usize] { | ||
if *is_out { | ||
*res += 1; | ||
} | ||
Self::dfs(*next, visited, res, graph); | ||
} | ||
} | ||
} | ||
pub fn min_reorder(n: i32, connections: Vec<Vec<i32>>) -> i32 { | ||
let mut res = 0; | ||
let mut visited = vec![false; n as usize]; | ||
let mut graph = vec![vec![]; n as usize]; | ||
for connection in connections { | ||
graph[connection[0] as usize].push((connection[1], true)); | ||
graph[connection[1] as usize].push((connection[0], false)); | ||
} | ||
Self::dfs(0, &mut visited, &mut res, &graph); | ||
res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
tests/graphs/reorder_routes_to_make_all_paths_lead_to_the_city_zero_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use rust_practice::graphs::reorder_routes_to_make_all_paths_lead_to_the_city_zero::Solution; | ||
|
||
#[test] | ||
fn min_reorder() { | ||
// 示例 1: | ||
// 输入:n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]] | ||
// 输出:3 | ||
// 解释:更改以红色显示的路线的方向,使每个城市都可以到达城市 0 。 | ||
let n = 6; | ||
let connections = vec![vec![0, 1], vec![1, 3], vec![2, 3], vec![4, 0], vec![4, 5]]; | ||
assert_eq!(Solution::min_reorder(n, connections), 3); | ||
|
||
// 示例 2: | ||
// 输入:n = 5, connections = [[1,0],[1,2],[3,2],[3,4]] | ||
// 输出:2 | ||
// 解释:更改以红色显示的路线的方向,使每个城市都可以到达城市 0 。 | ||
let n = 5; | ||
let connections = vec![vec![1, 0], vec![1, 2], vec![3, 2], vec![3, 4]]; | ||
assert_eq!(Solution::min_reorder(n, connections), 2); | ||
|
||
// 示例 3: | ||
// 输入:n = 3, connections = [[1,0],[2,0]] | ||
// 输出:0 | ||
let n = 3; | ||
let connections = vec![vec![1, 0], vec![2, 0]]; | ||
assert_eq!(Solution::min_reorder(n, connections), 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters