Problems > Any Cycle at All > Editorial

Any Cycle at All — Solution & Editorial

Back to the Problem

Union-find over the edges: an edge whose endpoints are already in the same set closes a cycle. DFS with parent tracking also works but multi-edges need care.

Complexity: O(M α(N))