Graph Theory37 sections · 1633 units
Open in Course

Problem - Round Trip

CSES undirected cycle

You have N cities and M roads (undirected). Find a cycle where you visit at least 33 cities and return to the start. Print the cycle or report IMPOSSIBLE.

This looks similar to cycle detection, but there is a twist: undirected graphs.

What changes when edges go both ways? In undirected graphs, every edge creates a trivial back-and-forth, which is not a real cycle. You need to track the parent of each node during DFS to avoid counting the edge you came from as a back edge.