Process deadlock detection
Your task
An operating system tracks a wait-for graph among n processes labeled 0..n-1. Each edge [a, b] means process a is blocked waiting for a resource currently held by process b. A deadlock exists if and only if there is a cycle in this directed graph. Return True if the system is deadlocked, otherwise False. The graph may have many edges and may be disconnected; self-loops [a, a] count as a deadlock.
Examples
has_deadlock(n: int, waits: list[list[int]]) -> boolExample 1
Input
has_deadlock(3, [[0, 1], [1, 2], [2, 0]])Output
True