-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycleInDirectedGraph.cpp
More file actions
50 lines (49 loc) · 1.57 KB
/
cycleInDirectedGraph.cpp
File metadata and controls
50 lines (49 loc) · 1.57 KB
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
#include<iostream>
#include<vector>
#include<queue>
#include<unordered_set>
bool dfs(std::vector<std::vector<size_t> > &adjacencyMatx, std::unordered_set<size_t> &whiteSet, std::unordered_set<size_t> &greySet, std::unordered_set<size_t> &blackSet,int currVertex){
whiteSet.erase(currVertex);
greySet.insert(currVertex);
for(size_t neighbourVertex: adjacencyMatx[currVertex]){
if(greySet.find(neighbourVertex)!=greySet.end()){
return true;
}
if(blackSet.find(neighbourVertex)!=blackSet.end()){
continue;
}
if(dfs(adjacencyMatx,whiteSet,greySet,blackSet,neighbourVertex)){
return true;
}
}
greySet.erase(currVertex);
blackSet.insert(currVertex);
return false;
}
int solve(int A,std::vector<std::vector<int> >&B){
std::vector<bool> visited(A+1, false);
//make an adjacency List
std::vector<std::vector<size_t> > adjacencyList(A+1);
for(int rowIndex = 0;rowIndex<B.size();++rowIndex){
int startVertex = B[rowIndex][0];
int endVertex = B[rowIndex][1];
adjacencyList[startVertex].push_back(endVertex);
}
//adjacencyList is ready
//use dfs
std::unordered_set<size_t> whiteSet, greySet,blackSet;
for(int vertex = 1;vertex<=A;++vertex){
whiteSet.insert(vertex);
}
for(size_t vertex = 1;vertex<=A;++vertex){
if(whiteSet.find(vertex)!=whiteSet.end()){
if(dfs(adjacencyList,whiteSet,greySet,blackSet,vertex)){
return 1;
}
}
}
return 0;
}
int main(){
return 0;
}