Potemkin cycle
Time limit1sMemory limit256 MB
Find an induced cycle of length at least four in an undirected graph and print the canonically smallest one, or print no.
- Level
Medium6 of 10
- Topics
- Graph, BFS, Shortest path
- Solved
- No attempts yet
Problem
Prince Potemkin is known for his fake villages, put up in a hurry to impress visiting dignitaries. He leads a delegation along a closed route through his territory, and at every suitable site a troupe of actors raises a portable village and plays its inhabitants. As soon as the delegation moves on, the actors take the village apart and run ahead to the next site of the route.
Choosing the route takes some care. Members of the delegation sometimes leave the planned route for a short inspection trip, and the trick fails the moment they return to a site they have already seen, because they would find an empty field where a village stood. The route also has to pass through at least four sites to make an impression.
You are given a map of the territory, that is the list of two way direct roads between the suitable sites. An elaborate system of overpasses carries one road over another, so the dignitaries cannot change roads anywhere except at the two ends of a road.
Find a sequence of sites such that:
- ,
- all sites are different, that is for all ,
- a direct road joins and for , and a direct road joins and ,
- no other direct road runs between two sites of the sequence, that is and are not joined by a direct road for every with and .
Input
The first line has two integers and (, ), the number of sites and the number of direct roads. The sites are numbered 1 to . Each of the next lines has two different integers and (), meaning that a direct road joins the sites and . At most one road joins any two sites.
Output
If no sequence satisfies the conditions, print no.
Several sequences usually satisfy the conditions, so print the single one that the following rule picks, on one line, with the site numbers separated by single spaces.
For a site , let be what is left of the map after deleting together with every site that a road joins to . Two sites and form a detour pair of when a road joins to , a road joins to , no road joins and , and some path leads from to whose interior sites all belong to .
- Let be the smallest site that has a detour pair. If no site has one, print
no. - Write each detour pair of as with . Take the pair with the smallest , and among those the pair with the smallest .
- Let be the map restricted to , and the sites of , keeping every road between two of them. Among the shortest paths from to in , take the lexicographically smallest one: start at and always step to the smallest numbered site that keeps the remaining distance to as small as possible.
- Print , then , then the interior sites of that path in order, then .
The sequence built this way always satisfies the four conditions, and the rule leaves exactly one answer.