-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcourse-schedule.rs
66 lines (53 loc) · 1.52 KB
/
course-schedule.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
struct Node {
/// 入度个数
indegrees: i32,
/// 出度列表
adjacency: Vec<i32>,
}
impl Solution {
/// 有向图
/// 看是否有环
/// m[a] = b, a为课程编号,b为依赖a的课程
pub fn can_finish(num_courses: i32, prerequisites: Vec<Vec<i32>>) -> bool {
if prerequisites.is_empty() {
return true;
}
let mut h = std::collections::HashMap::new();
for i in prerequisites.iter() {
h.entry(i[1])
.or_insert(Node {
indegrees: 0,
adjacency: vec![],
})
.adjacency
.push(i[0]);
h.entry(i[0])
.or_insert(Node {
indegrees: 0,
adjacency: vec![],
})
.indegrees += 1;
}
let mut stack = vec![];
for (&i, node) in h.iter() {
if node.indegrees == 0 {
stack.push(i);
}
}
let mut count = 0;
while let Some(x) = stack.pop() {
count += 1;
let node = h.remove(&x).unwrap();
for i in node.adjacency {
h.entry(i).and_modify(|x| x.indegrees -= 1);
if h.get(&i).unwrap().indegrees == 0 {
stack.push(i);
}
}
}
h.is_empty() || count >= num_courses
}
}