Course Schedule IV in C++


Suppose there are a total of n courses we can to take, the courses are labeled from 0 to n-1.

Some courses may have direct prerequisites, as example, to take course 0 we have first to take course 1, which is expressed as a pair: [1,0].

So, if we have a number of courses n, a list of direct prerequisite pairs and a list of queries pairs.

You should find the answer for each queries[i] whether the course queries[i][0] is a prerequisite of the course queries[i][1] or not. Finally, we have to return a list of boolean, the answers to the given queries.

We have to keep in mind that if course a is a prerequisite of course b and course b is a prerequisite of course c, then, course a is a prerequisite of course c.

So, if the input is like n = 3, prerequisites = [[1,2],[1,0],[2,0]], queries = [[1,0],[1,2]], then the output will be [true,true]

To solve this, we will follow these steps −

  • N := 110

  • Define an array ret

  • Define one map in

  • for each element it in v, do

    • insert it[1] at the end of graph[it[0]]

    • (increase in[it[1]] by 1)

  • Define one queue q

  • for initialize i := 0, when i < n, update (increase i by 1), do −

    • if in[i] is same as 0, then −

      • insert i into q

  • Define one map idx

  • for initialize lvl := 1, when not q is empty, update (increase lvl by 1), do −

    • sz := size of q

    • while sz is not 0, decrease sz in each iteration, do −

      • node := first element of q

      • delete element from q

      • for each element it in graph[node]

        • (decrease in[it] by 1)

        • for each element x in c[node], do

          • insert x into c[it]

        • insert node into c[it]

        • if in[it] is same as 0, then −

          • insert it into q

  • for each element it in x, do

    • insert (frequency of it[0] in c[it[1]]) at the end of ret

  • return ret

Example 

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<bool> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
const int N = 110;
class Solution {
public:
   vector <int> graph[N];
   map <int, set <int>> c;
   vector<bool> checkIfPrerequisite(int n, vector<vector<int>>& v, vector<vector<int>>& x) {
      vector<bool> ret;
      map<int, int> in;
      for (auto& it : v) {
         graph[it[0]].push_back(it[1]);
         in[it[1]]++;
      }
      queue<int> q;
      for (int i = 0; i < n; i++) {
         if (in[i] == 0)
            q.push(i);
      }
      map<int, int> idx;
      for (int lvl = 1; !q.empty(); lvl++) {
         int sz = q.size();
         while (sz--) {
            int node = q.front();
            q.pop();
            for (auto& it : graph[node]) {
               in[it]--;
               for (auto& x : c[node])
                  c[it].insert(x);
               c[it].insert(node);
               if (in[it] == 0) {
                  q.push(it);
               }
            }
         }
      }
      for (auto& it : x) {
         ret.push_back(c[it[1]].count(it[0]));
      }
      return ret;
   }
};
main(){
   Solution ob;
   vector<vector<int>> prerequisites = {{1,2},{1,0},{2,0}}, queries = {{1,0},{1,2}};
   print_vector(ob.checkIfPrerequisite(3, prerequisites, queries));
}

Input

3, {{1,2},{1,0},{2,0}}, {{1,0},{1,2}}

Output

[1, 1, ]

Updated on: 18-Nov-2020

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements