-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0210.cpp
More file actions
28 lines (25 loc) · 706 Bytes
/
Copy path0210.cpp
File metadata and controls
28 lines (25 loc) · 706 Bytes
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
class Solution {
public:
vector<int> findOrder(int n, vector<vector<int>> &prerequisites) {
vector<vector<int>> adj(n);
vector<int> count(n, 0);
vector<int> res;
int num = 0;
for (auto &p : prerequisites) {
adj[p[1]].push_back(p[0]);
count[p[0]]++;
}
queue<int> q;
for (int i = 0; i < n; i++)
if (!count[i]) q.push(i);
while (!q.empty()) {
int root = q.front();
q.pop();
res.push_back(root);
n--;
for (int c : adj[root])
if (!--count[c]) q.push(c);
}
return n == 0 ? res : vector<int>();
}
};