Clone graph with canonical ids

Write codeMedium30 minMeta style

Your task

An undirected graph is given as an adjacency list: adj[i] is the list of neighbor indices of node i (0..n-1). Produce a deep clone and return its adjacency list, but with node indices remapped to a canonical order: perform a BFS from node 0 and assign new ids 0,1,2,... in the order nodes are first discovered (neighbors explored in ascending original-index order). Return the cloned graph's adjacency list under the new ids, with each neighbor list sorted ascending. If n is 0 return an empty list. The graph may be disconnected; nodes unreachable from 0 are appended afterward in ascending original-index order.

Examples

clone_relabel(adj: list[list[int]]) -> list[list[int]]
Example 1
Inputclone_relabel([[1, 2], [0, 2], [0, 1]])
Output[[1, 2], [0, 2], [0, 1]]