// return true if node idx is a safe node booldfs(int idx, vector<vector<int>>& graph){ if (isSafeNode.count(idx)) { return isSafeNode[idx]; }
bool isSafe = true; traveled.insert(idx);
if (!graph[idx].empty()) { for (constauto& nextNode : graph[idx]) { // if there have any loops, // this node must not be a safe node if (traveled.count(nextNode) || !dfs(nextNode, graph)) { isSafe = false; break; } } }