-
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 2360: Longest Cycle in a Graph
- Loading branch information
Showing
3 changed files
with
91 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
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,67 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::mem; | ||
|
||
impl Solution { | ||
pub fn longest_cycle(edges: Vec<i32>) -> i32 { | ||
const MAX_BIT: u32 = 1 << 31; | ||
|
||
let mut edges = edges; | ||
let n = edges.len(); | ||
let mut result = 0; | ||
let mut current_id = MAX_BIT - 1; | ||
|
||
for i in 0..n { | ||
let mut state = &mut edges[i]; | ||
|
||
if ((*state + 1) as u32) < MAX_BIT { | ||
current_id += 1; | ||
|
||
let start_id = current_id; | ||
|
||
loop { | ||
let next = mem::replace(state, current_id as _) as usize; | ||
|
||
if let Some(next_state) = edges.get_mut(next) { | ||
if ((*next_state + 1) as u32) < MAX_BIT { | ||
state = next_state; | ||
current_id += 1; | ||
|
||
continue; | ||
} | ||
|
||
if *next_state as u32 >= start_id { | ||
result = result.max(current_id - *next_state as u32); | ||
} | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
if result == 0 { | ||
-1 | ||
} else { | ||
(result + 1) as _ | ||
} | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn longest_cycle(edges: Vec<i32>) -> i32 { | ||
Self::longest_cycle(edges) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,23 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn longest_cycle(edges: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[3, 3, 4, 2, 3] as &[_], 3), | ||
(&[2, -1, 3, 1], -1), | ||
(&[3, 3, 4, 2, 3], 3), | ||
(&[4, 3, 3, 4, 7, 2, 3, 3], 3), | ||
]; | ||
|
||
for (edges, expected) in test_cases { | ||
assert_eq!(S::longest_cycle(edges.to_vec()), expected); | ||
} | ||
} | ||
} |