The Restless Cat
Time limit2sMemory limit512 MB
In a connected, planar, simple graph with a guaranteed cycle, find rooms whose single-vertex deletion leaves the graph acyclic (a forest), summing their indices.
- Level
Hard8 of 10
- Topics
- Graph, DFS, Implementation
- Solved
- No attempts yet
Problem
Cheolmin keeps a cat. He built a multi-level playground for it, and the playground has rooms and corridors. The rooms are numbered 1 through . Each corridor joins two different rooms and can be walked in both directions. At most one corridor joins any given pair of rooms. Every room is reachable from every other room along corridors. The playground is built in three dimensions, so no two corridors cross. The picture below shows a playground with 4 rooms and 5 corridors.

The cat is very restless and runs around the playground without stopping. It likes to pick distinct rooms , fix an order , and then run through that order again and again, so it visits in turn. For such a run, and , and , ..., and must each be joined by a corridor.
Cheolmin worries that the cat tires itself out, so he wants to leave no repeating run at all. To keep the work small, he removes exactly one room and closes the corridors attached to it.
In the playground above the cat can run rooms 1, 2, 3 in that order, and it can also run rooms 1, 2, 4, 3 in that order. Removing room 2 leaves no repeating run. Removing room 3 gives the same result. Removing room 4 still leaves a repeating run.
You are given the rooms and the corridors. Find the number of a room whose removal destroys every repeating run. If several rooms work, find the sum of their numbers. If no room works, the answer is 0.
Input
The first line contains the number of rooms and the number of corridors , separated by a space. ,
Each of the next lines contains the numbers of the two different rooms joined by one corridor. No pair of rooms appears more than once. The given playground has at least one repeating run.
Output
If removing a single room destroys every repeating run, print the number of that room. If several rooms work, print the sum of their numbers. If no room works, print 0.
Note
In the first example, removing room 2 works and removing room 3 works, so the answer is the sum 5.