-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210.cpp
More file actions
49 lines (41 loc) · 1.12 KB
/
Copy path210.cpp
File metadata and controls
49 lines (41 loc) · 1.12 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
46
47
48
49
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
int n = numCourses;
int* d = new int[n];
for(int i = 0; i < n; ++i)
d[i] = 0;
vector< vector<int> > e(n);
for(int i = 0; i < prerequisites.size(); ++i)
{
e[prerequisites[i][1]].push_back(prerequisites[i][0]);
d[prerequisites[i][0]]++;
}
queue<int> q;
for(int i = 0; i < n; ++i)
{
if(d[i] == 0)
{
q.push(i);
}
}
vector<int> ret;
while(!q.empty())
{
int t = q.front();
q.pop();
ret.push_back(t);
//别重复推了
//d[t] = -1;
for(int j = 0; j < e[t].size(); ++j)
{
if(--d[e[t][j]] == 0)
{
q.push(e[t][j]);
}
}
}
if(ret.size() != n)ret.clear();
return ret;
}
};