File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn can_finish ( num_courses : i32 , prerequisites : Vec < Vec < i32 > > ) -> bool {
3
+ let mut graph = vec ! [ vec![ ] ; num_courses as usize ] ;
4
+ let mut visited = vec ! [ 0 ; num_courses as usize ] ;
5
+
6
+ for edge in prerequisites {
7
+ graph[ edge[ 0 ] as usize ] . push ( edge[ 1 ] as usize ) ;
8
+ }
9
+
10
+ for i in 0 ..num_courses as usize {
11
+ if !Self :: dfs ( i, & graph, & mut visited) {
12
+ return false ;
13
+ }
14
+ }
15
+
16
+ true
17
+ }
18
+
19
+ fn dfs ( i : usize , graph : & Vec < Vec < usize > > , visited : & mut Vec < i32 > ) -> bool {
20
+ if visited[ i] == 1 {
21
+ return false ;
22
+ }
23
+
24
+ if visited[ i] == -1 {
25
+ return true ;
26
+ }
27
+
28
+ visited[ i] = 1 ;
29
+
30
+ for j in graph[ i] . iter ( ) {
31
+ if !Self :: dfs ( * j, graph, visited) {
32
+ return false ;
33
+ }
34
+ }
35
+
36
+ visited[ i] = -1 ;
37
+
38
+ true
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments