Alert incident clustering

Write codeEasy20 minServiceNow style

Your task

An incident-management tool clusters n alerts (0..n-1) that are linked by edges [a, b] meaning the two alerts are correlated. Two alerts are in the same incident if they are connected directly or transitively. Return a list label of length n where label[i] is the incident id of alert i. Number incidents 0, 1, 2, ... in the order their lowest-numbered alert first appears (so alert 0 is always in incident 0).

Examples

relabel_components(n: int, edges: list[list[int]]) -> list[int]
Example 1
Inputrelabel_components(5, [[0, 1], [3, 4]])
Output[0, 0, 1, 2, 2]