safe[root] = s;
continue;
}
if (visited[root]) continue;
if (visited[root]) {
st.pop();
continue;
};
visited[root] = true;
st.push(root);
st.push(-1);
for (int c : graph[root])
if (!visited[c]) st.push(c);
}
}
vector<int> res;
for (int i = 0; i < n; i++)
if (safe[i]) res.push_back(i);
return res;
}
};
// Cleaner code, more memory
class Solution {
int safe[100001] = {0}, count[10001] = {0};
public:
vector<int> eventualSafeNodes(const vector<vector<int>> &graph) {
int n = graph.size();
vector<vector<int>> adj(n);
for (int i = 0; i < n; i++) {
count[i] += graph[i].size();
for (int node : graph[i]) adj[node].push_back(i);
}
queue<int> q;
for (int i = 0; i < n; i++)
if (!count[i]) q.push(i);
while (!q.empty()) {
int root = q.front();
q.pop();
safe[root] = true;
for (auto node : adj[root])
if (!--count[node]) q.push(node);
}
vector<int> res;
for (int i = 0; i < graph.size(); i++)
if (safe[i]) res.push_back(i);
return res;
}
};