Skip to the code
cf.
+10 XP

#409 Pages Reachable From the Home Page

easy

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.

Example 1

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.

Example 2

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.

Constraints

  • 1 <= n <= 10000
  • 0 <= links.length <= 50000
  • 0 <= a, b < n
Saved in this browser

Run 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).