Skip to content

Commit 16c8769

Browse files
committed
279
1 parent d148108 commit 16c8769

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

279.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
bool iscycle(vector<int> adj[],vector<int> &vis,int id){
4+
if(vis[id]==1)
5+
return true;
6+
if(vis[id]==0){
7+
vis[id]=1;
8+
for(auto edge : adj[id]){
9+
if(iscycle(adj,vis,edge))
10+
return true;
11+
}
12+
}
13+
vis[id] = 2;
14+
return false;
15+
}
16+
17+
bool canFinish(int n, vector<vector<int>>& pre) {
18+
vector<int> adj[n];
19+
for(auto edge : pre)
20+
adj[edge[1]].push_back(edge[0]);
21+
vector<int> vis(n,0);
22+
23+
for(int i=0;i<n;i++){
24+
if(iscycle(adj,vis,i))
25+
return false;
26+
}
27+
return true;
28+
}
29+
};

0 commit comments

Comments
 (0)