-
Notifications
You must be signed in to change notification settings - Fork 7
/
207.cpp
29 lines (26 loc) · 864 Bytes
/
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
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<vector<int> > graph(numCourses);
vector<int> indegrees(numCourses,0); //顶点的入度,即有多少边指向该顶点
for(auto p : prerequisites){
indegrees[p.first]++;
graph[p.second].push_back(p.first);
}
deque<int> q;
for(int v = 0;v < numCourses;v++)
if(indegrees[v] == 0)
q.push_back(v);
while(!q.empty()){
int v = q.front();
q.pop_front();
for(int v2 : graph[v])
if(--indegrees[v2] == 0)
q.push_back(v2);
}
for(int indegree : indegrees)
if(indegree)
return false;
return true;
}
};