Commit a9ecb3f5 authored by zhayao's avatar zhayao
Browse files

still not working but close

parent 126f2299
......@@ -726,13 +726,44 @@ public:
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<bool> vect(numCourse, true);
cout<<"here"<<endl;
vector<vector<int>> graph = graph_maker(numCourses, prerequisites);
cout<<"here"<<endl;
stack<int> visiting;
unordered_set<int> set_visiting;
unordered_set<int> visited;
visiting.push(0);
set_visiting.insert(0);
while(!set_visiting.empty()){
int current = visiting.top();
cout<<current<<endl;
visiting.pop();
set_visiting.erase(current);
visited.insert(current);
for(int i=0; i<graph[current].size(); i++){
int neigh = graph[current][i];
if(visited.find(neigh) != visited.end() || visited.find(neigh) != set_visiting.end()) return false;
visiting.push(neigh);
set_visiting.insert(neigh);
}
}
return true;
}
vector<vector<int>> graph_maker(vector<vector<int>>& prerequisites){
vector<vector<int>> graph(prerequisites.size());
for(auto v: prerequisites){
graph[v[0]].push_back(v[1]]);
}
return graph;
}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment