C++ dfs Topological sort


#1
bool dfs(vector<bool> &done,vector<bool>&vis,vector<vector<int>>&adj,int node){
    vis[node]=true;
    bool ok=true;
    for(int e:adj[node]){
      if(vis[e]&&!done[e])return false;
      if(!vis[e]){
          ok=ok&dfs(done,vis,adj,e);
      }
    }
    done[node]=true;
    return ok;
}
int Solution::solve(int A, vector<int> &B, vector<int> &C) {
    vector<vector<int>> adj(A+1);
    for(int i=0;i<C.size();i++){
        adj[B[i]].push_back(C[i]);
    }
    vector<bool>  done(A+1),vis(A+1);
    int n=A;
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            bool canGo=dfs(done,vis,adj,i);
            if(!canGo)return 0;
        }
    }
    return 1;
}