Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ec0b8a3

Browse files
authored
feat: add rust solution to lc problem: No.0785 (doocs#1194)
1 parent f67f6c0 commit ec0b8a3

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

‎solution/0700-0799/0785.Is Graph Bipartite/README.md‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,95 @@ public:
301301
};
302302
```
303303
304+
### **Rust**
305+
306+
染色法:
307+
308+
```rust
309+
impl Solution {
310+
#[allow(dead_code)]
311+
pub fn is_bipartite(graph: Vec<Vec<i32>>) -> bool {
312+
let mut graph = graph;
313+
let n = graph.len();
314+
let mut color_vec: Vec<usize> = vec![0; n];
315+
for i in 0..n {
316+
if color_vec[i] == 0 && !Self::traverse(i, 1, &mut color_vec, &mut graph) {
317+
return false;
318+
}
319+
}
320+
true
321+
}
322+
323+
#[allow(dead_code)]
324+
fn traverse(v: usize, color: usize, color_vec: &mut Vec<usize>, graph: &mut Vec<Vec<i32>>) -> bool {
325+
color_vec[v] = color;
326+
for n in graph[v].clone() {
327+
if color_vec[n as usize] == 0 {
328+
// This node hasn't been colored
329+
if !Self::traverse(n as usize, 3 - color, color_vec, graph) {
330+
return false;
331+
}
332+
} else if color_vec[n as usize] == color {
333+
// The color is the same
334+
return false;
335+
}
336+
}
337+
true
338+
}
339+
}
340+
```
341+
342+
并查集:
343+
344+
```rust
345+
impl Solution {
346+
#[allow(dead_code)]
347+
pub fn is_bipartite(graph: Vec<Vec<i32>>) -> bool {
348+
let n = graph.len();
349+
let mut disjoint_set: Vec<usize> = vec![0; n];
350+
// Initialize the disjoint set
351+
for i in 0..n {
352+
disjoint_set[i] = i;
353+
}
354+
355+
// Traverse the graph
356+
for i in 0..n {
357+
if graph[i].is_empty() {
358+
continue;
359+
}
360+
let first = graph[i][0] as usize;
361+
for v in &graph[i] {
362+
let v = *v as usize;
363+
let i_p = Self::find(i, &mut disjoint_set);
364+
let v_p = Self::find(v, &mut disjoint_set);
365+
if i_p == v_p {
366+
return false;
367+
}
368+
// Otherwise, union the node
369+
Self::union(first, v, &mut disjoint_set);
370+
}
371+
}
372+
373+
true
374+
}
375+
376+
#[allow(dead_code)]
377+
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
378+
if d_set[x] != x {
379+
d_set[x] = Self::find(d_set[x], d_set);
380+
}
381+
d_set[x]
382+
}
383+
384+
#[allow(dead_code)]
385+
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
386+
let p_x = Self::find(x, d_set);
387+
let p_y = Self::find(y, d_set);
388+
d_set[p_x] = p_y;
389+
}
390+
}
391+
```
392+
304393
### **Go**
305394

306395
染色法:

‎solution/0700-0799/0785.Is Graph Bipartite/README_EN.md‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,95 @@ public:
220220
};
221221
```
222222
223+
### **Rust**
224+
225+
Graph coloring:
226+
227+
```rust
228+
impl Solution {
229+
#[allow(dead_code)]
230+
pub fn is_bipartite(graph: Vec<Vec<i32>>) -> bool {
231+
let mut graph = graph;
232+
let n = graph.len();
233+
let mut color_vec: Vec<usize> = vec![0; n];
234+
for i in 0..n {
235+
if color_vec[i] == 0 && !Self::traverse(i, 1, &mut color_vec, &mut graph) {
236+
return false;
237+
}
238+
}
239+
true
240+
}
241+
242+
#[allow(dead_code)]
243+
fn traverse(v: usize, color: usize, color_vec: &mut Vec<usize>, graph: &mut Vec<Vec<i32>>) -> bool {
244+
color_vec[v] = color;
245+
for n in graph[v].clone() {
246+
if color_vec[n as usize] == 0 {
247+
// This node hasn't been colored
248+
if !Self::traverse(n as usize, 3 - color, color_vec, graph) {
249+
return false;
250+
}
251+
} else if color_vec[n as usize] == color {
252+
// The color is the same
253+
return false;
254+
}
255+
}
256+
true
257+
}
258+
}
259+
```
260+
261+
Union find:
262+
263+
```rust
264+
impl Solution {
265+
#[allow(dead_code)]
266+
pub fn is_bipartite(graph: Vec<Vec<i32>>) -> bool {
267+
let n = graph.len();
268+
let mut disjoint_set: Vec<usize> = vec![0; n];
269+
// Initialize the disjoint set
270+
for i in 0..n {
271+
disjoint_set[i] = i;
272+
}
273+
274+
// Traverse the graph
275+
for i in 0..n {
276+
if graph[i].is_empty() {
277+
continue;
278+
}
279+
let first = graph[i][0] as usize;
280+
for v in &graph[i] {
281+
let v = *v as usize;
282+
let i_p = Self::find(i, &mut disjoint_set);
283+
let v_p = Self::find(v, &mut disjoint_set);
284+
if i_p == v_p {
285+
return false;
286+
}
287+
// Otherwise, union the node
288+
Self::union(first, v, &mut disjoint_set);
289+
}
290+
}
291+
292+
true
293+
}
294+
295+
#[allow(dead_code)]
296+
fn find(x: usize, d_set: &mut Vec<usize>) -> usize {
297+
if d_set[x] != x {
298+
d_set[x] = Self::find(d_set[x], d_set);
299+
}
300+
d_set[x]
301+
}
302+
303+
#[allow(dead_code)]
304+
fn union(x: usize, y: usize, d_set: &mut Vec<usize>) {
305+
let p_x = Self::find(x, d_set);
306+
let p_y = Self::find(y, d_set);
307+
d_set[p_x] = p_y;
308+
}
309+
}
310+
```
311+
223312
### **Go**
224313

225314
Graph coloring:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn is_bipartite(graph: Vec<Vec<i32>>) -> bool {
4+
let mut graph = graph;
5+
let n = graph.len();
6+
let mut color_vec: Vec<usize> = vec![0; n];
7+
for i in 0..n {
8+
if color_vec[i] == 0 && !Self::traverse(i, 1, &mut color_vec, &mut graph) {
9+
return false;
10+
}
11+
}
12+
true
13+
}
14+
15+
#[allow(dead_code)]
16+
fn traverse(v: usize, color: usize, color_vec: &mut Vec<usize>, graph: &mut Vec<Vec<i32>>) -> bool {
17+
color_vec[v] = color;
18+
for n in graph[v].clone() {
19+
if color_vec[n as usize] == 0 {
20+
// This node hasn't been colored
21+
if !Self::traverse(n as usize, 3 - color, color_vec, graph) {
22+
return false;
23+
}
24+
} else if color_vec[n as usize] == color {
25+
// The color is the same
26+
return false;
27+
}
28+
}
29+
true
30+
}
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /