Skip to content

feat: add rust solution to lc problem: No.0785 #1194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions solution/0700-0799/0785.Is Graph Bipartite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,95 @@ public:
};
```

### **Rust**

染色法:

```rust
impl Solution {
#[allow(dead_code)]
pub fn is_bipartite(graph: Vec<Vec<i32>>) -> bool {
let mut graph = graph;
let n = graph.len();
let mut color_vec: Vec<usize> = vec![0; n];
for i in 0..n {
if color_vec[i] == 0 && !Self::traverse(i, 1, &mut color_vec, &mut graph) {
return false;
}
}
true
}

#[allow(dead_code)]
fn traverse(v: usize, color: usize, color_vec: &mut Vec<usize>, graph: &mut Vec<Vec<i32>>) -> bool {
color_vec[v] = color;
for n in graph[v].clone() {
if color_vec[n as usize] == 0 {
// This node hasn't been colored
if !Self::traverse(n as usize, 3 - color, color_vec, graph) {
return false;
}
} else if color_vec[n as usize] == color {
// The color is the same
return false;
}
}
true
}
}
```

并查集:

```rust
impl Solution {
#[allow(dead_code)]
pub fn is_bipartite(graph: Vec<Vec<i32>>) -> bool {
let n = graph.len();
let mut disjoint_set: Vec<usize> = vec![0; n];
// Initialize the disjoint set
for i in 0..n {
disjoint_set[i] = i;
}

// Traverse the graph
for i in 0..n {
if graph[i].is_empty() {
continue;
}
let first = graph[i][0] as usize;
for v in &graph[i] {
let v = *v as usize;
let i_p = Self::find(i, &mut disjoint_set);
let v_p = Self::find(v, &mut disjoint_set);
if i_p == v_p {
return false;
}
// Otherwise, union the node
Self::union(first, v, &mut disjoint_set);
}
}

true
}

#[allow(dead_code)]
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
if d_set[x] != x {
d_set[x] = Self::find(d_set[x], d_set);
}
d_set[x]
}

#[allow(dead_code)]
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
let p_x = Self::find(x, d_set);
let p_y = Self::find(y, d_set);
d_set[p_x] = p_y;
}
}
```

### **Go**

染色法:
Expand Down
89 changes: 89 additions & 0 deletions solution/0700-0799/0785.Is Graph Bipartite/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,95 @@ public:
};
```

### **Rust**

Graph coloring:

```rust
impl Solution {
#[allow(dead_code)]
pub fn is_bipartite(graph: Vec<Vec<i32>>) -> bool {
let mut graph = graph;
let n = graph.len();
let mut color_vec: Vec<usize> = vec![0; n];
for i in 0..n {
if color_vec[i] == 0 && !Self::traverse(i, 1, &mut color_vec, &mut graph) {
return false;
}
}
true
}

#[allow(dead_code)]
fn traverse(v: usize, color: usize, color_vec: &mut Vec<usize>, graph: &mut Vec<Vec<i32>>) -> bool {
color_vec[v] = color;
for n in graph[v].clone() {
if color_vec[n as usize] == 0 {
// This node hasn't been colored
if !Self::traverse(n as usize, 3 - color, color_vec, graph) {
return false;
}
} else if color_vec[n as usize] == color {
// The color is the same
return false;
}
}
true
}
}
```

Union find:

```rust
impl Solution {
#[allow(dead_code)]
pub fn is_bipartite(graph: Vec<Vec<i32>>) -> bool {
let n = graph.len();
let mut disjoint_set: Vec<usize> = vec![0; n];
// Initialize the disjoint set
for i in 0..n {
disjoint_set[i] = i;
}

// Traverse the graph
for i in 0..n {
if graph[i].is_empty() {
continue;
}
let first = graph[i][0] as usize;
for v in &graph[i] {
let v = *v as usize;
let i_p = Self::find(i, &mut disjoint_set);
let v_p = Self::find(v, &mut disjoint_set);
if i_p == v_p {
return false;
}
// Otherwise, union the node
Self::union(first, v, &mut disjoint_set);
}
}

true
}

#[allow(dead_code)]
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
if d_set[x] != x {
d_set[x] = Self::find(d_set[x], d_set);
}
d_set[x]
}

#[allow(dead_code)]
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
let p_x = Self::find(x, d_set);
let p_y = Self::find(y, d_set);
d_set[p_x] = p_y;
}
}
```

### **Go**

Graph coloring:
Expand Down
31 changes: 31 additions & 0 deletions solution/0700-0799/0785.Is Graph Bipartite/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
impl Solution {
#[allow(dead_code)]
pub fn is_bipartite(graph: Vec<Vec<i32>>) -> bool {
let mut graph = graph;
let n = graph.len();
let mut color_vec: Vec<usize> = vec![0; n];
for i in 0..n {
if color_vec[i] == 0 && !Self::traverse(i, 1, &mut color_vec, &mut graph) {
return false;
}
}
true
}

#[allow(dead_code)]
fn traverse(v: usize, color: usize, color_vec: &mut Vec<usize>, graph: &mut Vec<Vec<i32>>) -> bool {
color_vec[v] = color;
for n in graph[v].clone() {
if color_vec[n as usize] == 0 {
// This node hasn't been colored
if !Self::traverse(n as usize, 3 - color, color_vec, graph) {
return false;
}
} else if color_vec[n as usize] == color {
// The color is the same
return false;
}
}
true
}
}