Implement cycle detector using DFS