visit
and inStack
, to keep track of visited nodes and nodes in the current DFS path.inStack
, it is part of a cycle and marked as unsafe.inStack
, it is safe and DFS continues.inStack[node] = false
.inStack
to the result list as they are safe.