Generalized DFS - detect cycles in directed graphs