-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourse_Schedule.cpp
More file actions
45 lines (43 loc) · 1.05 KB
/
Course_Schedule.cpp
File metadata and controls
45 lines (43 loc) · 1.05 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
class Solution {
public:
void DFSUtil(vector<int>vec[],int u,vector<int>&visited,bool &flag)
{
for(int i=0;i<vec[u].size();i++)
{ if(visited[vec[u][i]]==0)
{ visited[vec[u][i]]=2;
DFSUtil(vec,vec[u][i],visited,flag);
visited[vec[u][i]]=1;
}
else if(visited[vec[u][i]]==2)
{
flag=true;
return;
}
}
}
bool DFS(vector<int>vec[],int n)
{
vector<int>visited(n,0);
bool flag=false;
for(int i=0;i<n;i++)
{
visited[i]=2;
DFSUtil(vec,i,visited,flag);
if(flag==true)
return false;
visited[i]=1;
}
return true;
}
bool canFinish(int n, vector<vector<int>>& p) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<int>vec[n];
for(int i=0;i<p.size();i++)
{
vec[p[i][0]].push_back(p[i][1]);
}
return DFS(vec,n);
}
};