forked from ShyrenMore/InterviewPrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourse_1.cpp
43 lines (37 loc) · 1.01 KB
/
course_1.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// https://practice.geeksforgeeks.org/problems/prerequisite-tasks/1
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
bool isPossible(int V, vector<pair<int, int>> &prerequisites)
{
// Code here
vector<int> adj[V];
for (auto it : prerequisites)
adj[it.second].push_back(it.first);
int indegree[V] = {0};
for (int node = 0; node < V; node++)
for (auto it : adj[node])
indegree[it]++;
queue<int> q;
for (int i = 0; i < V; i++)
if (indegree[i] == 0)
q.push(i);
vector<int> topo;
while (!q.empty())
{
int node = q.front();
q.pop();
topo.push_back(node);
for (auto it : adj[node])
{
indegree[it]--;
if (indegree[it] == 0)
q.push(it);
}
}
return (topo.size() == V) ? true : false;
// return topo;
}
};