-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path207.cpp
More file actions
27 lines (27 loc) · 833 Bytes
/
Copy path207.cpp
File metadata and controls
27 lines (27 loc) · 833 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
class Solution {
public:
bool canFinish(int n, vector<pair<int, int>> &prerequisites) {
vector<unordered_set<int>> graph(n, unordered_set<int>());
vector<int> in_degree(n, 0);
for (int i = 0; i < prerequisites.size(); ++i)
graph[prerequisites[i].second].insert(prerequisites[i].first);
for (int i = 0; i < n; ++i)
for (auto in:graph[i])
++in_degree[in];
for (int i = 0; i < n; ++i) {
int j = 0;
while (j < n) {
if (in_degree[j] == 0)
break;
++j;
}
if (j == n)
return false;
in_degree[j] = -1;
for (auto &post:graph[j]) {
--in_degree[post];
}
}
return true;
}
};