Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int dfs(vector<int> *a,vector<int> &vis,vector<int> &rec,int x)
- {
- vis[x]=1;
- rec[x]=1;
- for(int i:a[x])
- {
- if(vis[i]==1 && rec[i]==1)
- return 1;
- if(vis[i]==0 && dfs(a,vis,rec,i))
- return 1;
- }
- rec[x]=0;
- return 0;
- }
- bool canFinish(int n, vector<vector<int>>& e) {
- vector<int> a[n];
- for(int i=0;i<e.size();i++)
- {
- a[e[i][0]].push_back(e[i][1]);
- }
- vector<int> vis(n,0);
- vector<int> rec(n,0);
- for(int i=0;i<n;i++)
- {
- if(vis[i]==0)
- {
- int x=dfs(a,vis,rec,i);
- if(x==1)
- return 0;
- }
- }
- return 1;
- }
- };
- // to complete all courses there shouldn't be any cycle
- // if there is a cycle then we cannot complete all course
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement