Untitled
unknown
plain_text
2 years ago
621 B
9
Indexable
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;
}
};Editor is loading...
Leave a Comment