-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay29.cpp
63 lines (48 loc) · 1.13 KB
/
Day29.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// cycle finding in Directed Graph
class Solution
{
vector < int> graph[100000];
int visited[100000];
bool flag = false;
void dfs(int u, int visited[])
{
visited[u] = -1;
for(auto x:graph[u])
{
if(visited[x]==0)
{
dfs(x,visited);
}
else if(visited[x]==-1){
flag = true;
return;
}
}
visited[u] = 1;
}
public:
bool canFinish(int numCourses, vector<vector<int>>& arr)
{
for (auto x : arr)
{
vector <int> temp = x;
int a = temp[0];
int b = temp[1];
graph[b].push_back(a);
}
memset(visited,false,sizeof visited);
for(int i = 0 ; i < numCourses ; i++)
{
if( visited[i]==0 )
{
flag = false;
dfs(i,visited);
if(flag)
{
return false;
}
}
}
return true;
}
};