写点什么

2022-12-18:给定一个长度为 n 的二维数组 graph,代表一张图, graph[i] = {a,b,c,d} 表示 i 讨厌 (a,b,c,d),讨厌关系为双向的, 一共有 n 个人,编号 0~n-1, 讨

  • 2022-12-18
    北京
  • 本文字数:1191 字

    阅读完需:约 4 分钟

2022-12-18:给定一个长度为n的二维数组graph,代表一张图, graph[i] = {a,b,c,d} 表示i讨厌(a,b,c,d),讨厌关系为双向的, 一共有n个人,编号0~n-1, 讨

2022-12-18:给定一个长度为 n 的二维数组 graph,代表一张图,graph[i] = {a,b,c,d} 表示 i 讨厌(a,b,c,d),讨厌关系为双向的,一共有 n 个人,编号 0~n-1,讨厌的人不能一起开会。返回所有人能不能分成两组开会。来自微软面试。


答案 2022-12-18:


力扣 785。并查集。


代码用 rust 编写。代码如下:


use rand::Rng;use std::iter::repeat;fn main() {    let mut graph: Vec<Vec<i32>> = vec![vec![1, 2, 3], vec![0, 2], vec![0, 1, 3], vec![0, 2]];    let ans = is_bipartite(&mut graph);    println!("ans = {}", ans);}
fn is_bipartite(graph: &mut Vec<Vec<i32>>) -> bool { let n = graph.len() as i32; let mut uf = UnionFind::new(n); for neighbours in graph.iter() { for i in 1..neighbours.len() as i32 { uf.union(neighbours[(i - 1) as usize], neighbours[i as usize]); } } for i in 0..n { for j in graph[i as usize].iter() { if uf.same(i, *j) { return false; } } } return true;}
struct UnionFind { f: Vec<i32>, s: Vec<i32>, h: Vec<i32>,}impl UnionFind { pub fn new(n: i32) -> Self { let mut f: Vec<i32> = repeat(0).take(n as usize).collect(); let mut s: Vec<i32> = repeat(0).take(n as usize).collect(); let mut h: Vec<i32> = repeat(0).take(n as usize).collect(); for i in 0..n { f[i as usize] = i; s[i as usize] = 1; } Self { f, s, h } }
fn find(&mut self, mut i: i32) -> i32 { let mut hi = 0; while i != self.f[i as usize] { self.h[hi as usize] = i; hi += 1; i = self.f[i as usize]; } while hi > 0 { hi -= 1; self.f[self.h[hi as usize] as usize] = i; } return i; }
pub fn same(&mut self, i: i32, j: i32) -> bool { return self.find(i) == self.find(j); }
pub fn union(&mut self, i: i32, j: i32) { let mut fi = self.find(i); let mut fj = self.find(j); if fi != fj { if self.s[fi as usize] >= self.s[fj as usize] { self.f[fj as usize] = fi; self.s[fi as usize] = self.s[fi as usize] + self.s[fj as usize]; } else { self.f[fi as usize] = fj; self.s[fj as usize] = self.s[fi as usize] + self.s[fj as usize]; } } }}
复制代码


执行结果如下:





左神java代码

发布于: 刚刚阅读数: 3
用户头像

还未添加个人签名 2021-02-15 加入

还未添加个人简介

评论

发布
暂无评论
2022-12-18:给定一个长度为n的二维数组graph,代表一张图, graph[i] = {a,b,c,d} 表示i讨厌(a,b,c,d),讨厌关系为双向的, 一共有n个人,编号0~n-1, 讨_算法_福大大架构师每日一题_InfoQ写作社区