Counter-demonstration
Time limit2sMemory limit512 MB
Given a directed graph, decide whether it contains a cycle and list every intersection that lies on all cycles.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Topological sort
- Solved
- No attempts yet
Statement
Every year, Bitowice hosts the P = NP Equality Parade. It is an event where people who believe that for every language recognized in polynomial time by some nondeterministic Turing machine, there is also a deterministic Turing machine recognizing in polynomial time, express their views to the public. ∗ Previous parades were peaceful. Participants at most shouted "3-SAT is easy!" or held banners with pseudocode of the newest polynomial "algorithms" for finding Hamiltonian cycles, and passersby hardly noticed. This year the organizers want to draw the attention of Bitowice residents and plan to chant more shocking slogans (which are partly true if P = NP), for example "Our money is not safe!" and "Our privacy is under threat!". Officers of the Bitowice Security Agency (ABB) fear that the messages of the parade may lead residents to withdraw their money from banks en masse and delete their accounts on the social networks that ABB uses to monitor the population. In short, they suspect the situation in Bitowice will become unstable. To prevent this destabilization, ABB officers plan a counter-demonstration promoting belief in . At the same time, they plan to peacefully block the parade from passing. ABB intends to start the counter-demonstration suddenly at one of the intersections on the parade route. The exact route of the P = NP parade is kept secret until the very end, so the agency must prepare the counter-demonstration location in advance. ABB has only received a tip that the parade will start at a certain intersection, run along a nonzero number of roads, and finally return to its starting point. Your first task is to verify this information in advance, that is, check whether Bitowice's road network allows such a route to exist. The agents also want to know whether there are intersections that the parade route must pass through if the tip is confirmed. They asked you to find all such intersections. From them, they will choose the most convenient location for the counter-demonstration. If no such intersections exist, ABB will move to plan B. Bitowice has intersections connected by one-way roads. Since the parade also includes motor vehicles, we assume that the parade can move only in the direction of the roads. ∗ For more information on the P versus NP problem, see http://en.wikipedia.org/wiki/P_versus_NP_problem.
Input
The first line contains two integers and (, ), the number of intersections and the number of roads in Bitowice. Intersections are numbered from to . Each of the next lines describes a road. The -th line contains two integers and (, ), meaning that the -th road leads from intersection to intersection . No ordered pair appears more than once.
Output
If the parade cannot be organized along a route consistent with the information ABB has, print a single line containing NIE. Otherwise, print two lines. The first line contains the number of intersections that the parade route will certainly pass through. The second line contains the numbers of these intersections in increasing order. If , leave the second line empty.