Efficient solution using hashing with chaining. Graph DFS