-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrerequisite_tasks.cpp
More file actions
71 lines (65 loc) · 1.53 KB
/
Prerequisite_tasks.cpp
File metadata and controls
71 lines (65 loc) · 1.53 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
bool isPossible(int V, int P, vector<pair<int, int> >& prerequisites) {
vector<int> adj[V];
for(auto it : prerequisites)
{
adj[it.first].push_back(it.second);
}
int indegree[V] = {0};
for(int i = 0; i < V; i++)
{
for(auto it : adj[i])
indegree[it]++;
}
queue<int>q;
for(int i = 0; i < V; i++)
{
if(indegree[i] == 0)
q.push(i);
}
int cnt = 0;
while(!q.empty())
{
int node = q.front();
q.pop();
cnt++;
for(auto it : adj[node])
{
indegree[it]--;
if(indegree[it] == 0) q.push(it);
}
}
return cnt == V;
}
};
//{ Driver Code Starts.
int main(){
int tc;
cin >> tc;
while(tc--){
int N, P;
vector<pair<int, int> > prerequisites;
cin >> N;
cin >> P;
for (int i = 0; i < P; ++i) {
int x, y;
cin >> x >> y;
prerequisites.push_back(make_pair(x, y));
}
// string s;
// cin>>s;
Solution ob;
if (ob.isPossible(N,P, prerequisites))
cout << "Yes";
else
cout << "No";
cout << endl;
}
return 0;
}
// } Driver Code Ends