Use map for visited set