-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1136.cpp
More file actions
38 lines (38 loc) · 1018 Bytes
/
Copy path1136.cpp
File metadata and controls
38 lines (38 loc) · 1018 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
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
int minimumSemesters(int N, vector<vector<int>>& relations) {
vector<int> indegree(N+1,0);
vector<unordered_set<int>> graph(N+1);
for(auto& arr: relations){
graph[arr[0]].insert(arr[1]);
indegree[arr[1]]++;
}
queue<int> q;
for(int i=1;i<=N;i++){
if(indegree[i]==0){
q.push(i);
}
}
int res = 0;
int count = 0;
while(!q.empty()){
int size = q.size();
if(size!=0)
res++;
while(size--){
auto curr = q.front();q.pop();
count++;
for(auto v: graph[curr]){
if(indegree[v]>0){
indegree[v]--;
if(indegree[v]==0)
q.push(v);
}
}
}
}
if(count==N)
return res;
return -1;
}
};