From c4b04378de3b3f260ab1ead4df7193cff3252826 Mon Sep 17 00:00:00 2001 From: Kshitiz Sharma <110187324+kshitiz11101@users.noreply.github.com> Date: Wed, 29 May 2024 22:15:52 +0530 Subject: [PATCH] Create EventualSafeStates.cpp --- Graphs/EventualSafeStates.cpp | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Graphs/EventualSafeStates.cpp diff --git a/Graphs/EventualSafeStates.cpp b/Graphs/EventualSafeStates.cpp new file mode 100644 index 0000000..ee72e71 --- /dev/null +++ b/Graphs/EventualSafeStates.cpp @@ -0,0 +1,47 @@ +class Solution { + private: + bool dfs(int node, vector&vis,vector>& graph, vector&pathVis, vector&check){ + vis[node]=1; + pathVis[node]=1; + check[node]=0; + for(auto it:graph[node]){ + if(!vis[it]){ + if(dfs(it,vis,graph,pathVis,check)==true){ + check[node]=0; + return true; + } + } + else if(pathVis[it]){ + check[node]=0; + return true; + } + } + check[node]=1; + pathVis[node]=0; + return false; + + } +public: + vector eventualSafeNodes(vector>& graph) { + int V=graph.size(); + + vectorvis(V,0); + vectorpathVis(V,0); + vectorcheck(V,0); + vectorsafe; + + for(int i=0;i