-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path207.cpp
50 lines (43 loc) · 1.07 KB
/
207.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
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
queue<int> q;
map<int, vector<int> > g;
int* in = new int[numCourses];
for(int i = 0; i < numCourses; ++i)in[i] = 0;
for(int i = 0; i < prerequisites.size(); ++i)
{
int e = prerequisites[i][0];
int s = prerequisites[i][1];
g[s].push_back(e);
in[e]++;
}
for(int i = 0; i < numCourses; ++i)
{
if(in[i] == 0)
{
q.push(i);
}
}
while(!q.empty())
{
int s = q.front();
q.pop();
for(int i = 0; i < g[s].size(); ++i)
{
in[g[s][i]]--;
if(in[g[s][i]] == 0)q.push(g[s][i]);
}
}
bool flag = true;
for(int i = 0; i < numCourses; ++i)
{
if(in[i] > 0)
{
flag = false;
break;
}
}
return flag;
}
};