Open yokostan opened 5 years ago
Explanation for DFS adjacent map: In DFS, the general idea, is to search each vertex in the graph recursively, if the current vertex has been visited in this search, then there must be a cycle. Be careful with the status of each vertex, here in my code, it has three states: unvisited (=0), visiting(=1), visited(=2). The =1 state is to detect the existence of cycle, while the =2 state indicate that the vertex is already checked and there is no cycle.
Think about it like a forest, 2 means all elements that we could possibly reach in this tree has been visited and whenever we find another prerequisite course that leads to this tree, then it's fine. But if we are checking a tree with status 1 and have found duplicate element, then this tree is not gonna work.
Map & indegree map, runtime beats 38%:
Adjacent map, beat 84%:
Regular map beats 55%: