您的位置:首页 > 其它

207. Course Schedule

2017-09-11 08:31 267 查看
class Solution {
public:
static const int VISITING = 0;
static const int VISITED = 1;
static const int NOTVISITED = 2;
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {

int notVisited = NOTVISITED;
vector<int> stat(numCourses, notVisited);
vector<vector<int>> graph(numCourses, vector<int>());
for(const auto &i : prerequisites){
graph[i.second].push_back(i.first);
}

for(int i = 0; i < numCourses; ++i)
if(stat[i] == NOTVISITED && !dfs(stat, graph, i))
return false;
return true;
}

bool dfs(vector<int> &stat, const vector<vector<int>> &graph, int pos){
if(stat[pos] == VISITING)
return false;
stat[pos] = VISITING;
for(auto i : graph[pos]){
if(!dfs(stat, graph, i))
return false;
}
stat[pos] = VISITED;
return true;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode