A docs team wants readers to be able to get from any page to any other page by following links. Pages are numbered 0 to n - 1, and links[i] = [a, b] means page a links to page b.
Write fewestNewLinks(n, links) that returns the smallest number of new links to add so that every page can reach every other page. If that is already true, return 0.
Input: n = 3, links = [[0,1],[1,2]] Output: 1
Adding a link from page 2 back to page 0 closes the loop.
Input: n = 4, links = [[0,1],[2,3]] Output: 2
Pages 0 and 2 have no links in and pages 1 and 3 have no links out. Two links, 1 → 2 and 3 → 0, fix all of them.
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 #476 Fewest Links So Every Page Reaches Every 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.