Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
621 B
0
Indexable
Never
class Solution {
public:
    bool dfs(int i,unordered_map<int,bool>&vis,vector<vector<int>>& adj){
        if(vis.find(i)!=vis.end()) return vis[i];
        vis[i]=false;

        for(auto nbr:adj[i]){
            if(dfs(nbr,vis,adj)==false) return false;
        }
        return vis[i]=true;
    }

    vector<int> eventualSafeNodes(vector<vector<int>>& adj) {
        int n=adj.size();
        unordered_map<int,bool>vis;

        vector<int>res;
        for(int i=0;i<n;i++){
            if(dfs(i,vis,adj)) res.push_back(i);
        }        
       

        sort(res.begin(),res.end());
        return res;
    }
};
Leave a Comment