n nodes numbered 0 to n - 1, joined by two-way links. It is given as a list links, where links[i] holds every node directly connected to node i. The network is connected: any node can be reached from any other.
You want a walk that visits every node at least once. It may start and end anywhere, and it may revisit nodes and reuse links as often as needed.
Return the smallest number of moves along links such a walk can take.links=[[1], [0, 2], [1, 3], [2]] → 3
This is the chain 0–1–2–3: walking it end to end covers every node in 3 moves.
links=[[1, 2, 3], [0], [0], [0]] → 4
A star centered at 0: for example 1 → 0 → 2 → 0 → 3. The walk has to come back to the center, so it takes 4 moves.
links=[[]] → 0
The only node is visited at the start, so no moves are needed.
len(links) ≤ 12len(links[i]) < n, and links[i] has no repeats and does not contain ib is in links[a], then a is in links[b]links=[[1], [0, 2], [1, 3], [2]]
3
Run checks the first 3 tests in your browser. Submit checks all 46 tests on the server, hidden ones included.