-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2039.rs
45 lines (43 loc) · 1.49 KB
/
p2039.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use crate::lc::Solution;
use std::collections::VecDeque;
/// #VecDeque
impl Solution {
pub fn network_becomes_idle(edges: Vec<Vec<i32>>, patience: Vec<i32>) -> i32 {
let mut max = 0;
let mut graph = vec![vec![]; patience.len()];
for v in &edges {
graph[v[0] as usize].push(v[1]);
graph[v[1] as usize].push(v[0]);
}
let mut queue = VecDeque::new();
let mut visited = vec![false; patience.len()];
queue.push_back(0);
visited[0] = true;
let mut dist = 1;
while !queue.is_empty() {
let mut size = queue.len();
while size > 0 {
size -= 1;
let cur = queue.pop_front().unwrap();
for idx in &graph[cur] {
let v = *idx as usize;
if visited[v] {
continue;
}
visited[v] = true;
queue.push_back(v);
let time = dist * 2 + 1 + patience[v] * ((2 * dist - 1) / patience[v]);
max = max.max(time);
}
}
dist += 1;
}
max
}
}
#[test]
fn test() {
assert_eq!(8, Solution::network_becomes_idle(vec![vec![0,1],vec![1,2]], vec![0,2,1]));
assert_eq!(3, Solution::network_becomes_idle(vec![vec![0,1],vec![1,2],vec![0,2]], vec![0,10,10]));
assert_eq!(3, Solution::network_becomes_idle(vec![vec![0,1],vec![0,2],vec![1,2]], vec![0,10,10]));
}