-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2316: Count Unreachable Pairs of Nodes in an Undirected G…
…raph
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 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
70 changes: 70 additions & 0 deletions
70
src/problem_2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph/bfs.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,70 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::collections::VecDeque; | ||
|
||
impl Solution { | ||
pub fn count_pairs(n: i32, edges: Vec<Vec<i32>>) -> i64 { | ||
let n = n as u32 as usize; | ||
let mut nodes = vec![Vec::new(); n].into_boxed_slice(); | ||
|
||
for edge in edges { | ||
let [from, to]: [_; 2] = edge.try_into().ok().unwrap(); | ||
|
||
nodes[from as u32 as usize].push(to as u32); | ||
nodes[to as u32 as usize].push(from as u32); | ||
} | ||
|
||
let mut total_count = 0_u64; | ||
let mut result = 0; | ||
let mut visited = vec![false; n].into_boxed_slice(); | ||
let mut queue = VecDeque::new(); | ||
|
||
for mut node in 0..n { | ||
if let state @ false = &mut visited[node] { | ||
*state = true; | ||
|
||
let mut count = 1; | ||
|
||
loop { | ||
for &next in &nodes[node] { | ||
if let state @ false = &mut visited[next as usize] { | ||
*state = true; | ||
|
||
queue.push_back(next); | ||
} | ||
} | ||
|
||
if let Some(next) = queue.pop_front() { | ||
node = next as _; | ||
count += 1; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
result += total_count * count; | ||
total_count += count; | ||
} | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn count_pairs(n: i32, edges: Vec<Vec<i32>>) -> i64 { | ||
Self::count_pairs(n, edges) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/problem_2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph/mod.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,21 @@ | ||
pub mod bfs; | ||
|
||
pub trait Solution { | ||
fn count_pairs(n: i32, edges: Vec<Vec<i32>>) -> i64; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((3, &[[0, 1], [0, 2], [1, 2]] as &[_]), 0), | ||
((7, &[[0, 2], [0, 5], [2, 4], [1, 6], [5, 4]]), 14), | ||
]; | ||
|
||
for ((n, edges), expected) in test_cases { | ||
assert_eq!(S::count_pairs(n, edges.iter().map(Vec::from).collect()), expected); | ||
} | ||
} | ||
} |