Cache network distance

Write codeEasy17 min

Your task

A content network runs its own backbone. The map holds site_count sites numbered 0 upward, and its fibre spans live in two parallel lists of equal length: span i joins site link_a[i] to site link_b[i], and traffic runs both ways along a span. caches lists the sites holding a copy of the library, and a site may be listed there more than once. Return one entry per site, in ascending site number order, giving how many spans separate that site from the nearest cache. A site holding a cache itself answers 0, and a site that no chain of spans connects to any cache answers -1. A span listed twice adds nothing, and a span whose two ends name the same site changes nothing. Every number given names a real site, and a map with no sites returns an empty list.

Examples

hops_to_nearest_cache(site_count: int, link_a: list[int], link_b: list[int], caches: list[int]) -> list[int]
Example 1
Inputhops_to_nearest_cache(6, [0, 1, 1, 3, 4], [1, 2, 3, 4, 5], [0])
Output[0, 1, 2, 2, 3, 4]
Example 2
Inputhops_to_nearest_cache(5, [0, 1, 3], [1, 2, 4], [2, 3])
Output[2, 1, 0, 0, 1]
Example 3
Inputhops_to_nearest_cache(3, [], [], [])
Output[-1, -1, -1]