-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_dfs_v+e.cpp
49 lines (43 loc) · 1.09 KB
/
AC_dfs_v+e.cpp
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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dfs_v+e.cpp
* Create Date: 2015-08-10 19:36:45
* Descripton:
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
if (numCourses == 0 || prerequisites.empty())
return true;
graph = vector<vector<int> >(numCourses);
vis = vector<int>(numCourses, 0); // not visit
for (auto i : prerequisites) {
graph[i.second].push_back(i.first);
}
for (int u = 0; u < numCourses; ++u) {
if (0 == vis[u] && !dfs(u))
return false;
}
return true;
}
private:
vector<vector<int> > graph;
vector<int> vis;
bool dfs(int u) {
vis[u] = 1; // visiting
for (auto v : graph[u]) {
if (vis[v] == 1)
return false;
if (dfs(v) == false)
return false;
}
vis[u] = 2; // visited
return true;
};
};
int main() {
return 0;
}