A site audit wants to know how much of the site a visitor can reach by clicking links, starting from the home page. Pages are numbered 0 to n - 1, page 0 is the home page, and links[i] = [a, b] means page a links to page b.
Write countReachable(n, links) that returns how many pages can be reached from page 0 by following links, counting page 0 itself.
Input: n = 5, links = [[0,1],[1,2],[3,4],[2,0]] Output: 3
From page 0 you reach 1 and then 2. Pages 3 and 4 only link to each other.
Input: n = 4, links = [[1,0],[2,0]] Output: 1
Links point into the home page but none leave it, so only page 0 counts.
1 <= n <= 100000 <= links.length <= 500000 <= a, b < nRun tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #409 Pages Reachable From the Home Page. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.