#P3436. Longest Route to the Main Building

    ID: 16691 Type: Default 1000ms 256MiB

Longest Route to the Main Building

Longest Route to the Main Building

The Byteotian University is located in the city of Byteion. In addition to the main building, the university owns n cottages for its academic staff. There are one‐way alleys connecting these cottages, and there may be multiple alleys between any two cottages (even a loop from a building to itself is allowed). Alleys also connect cottages to the main building. Note that no two alleys intersect at any point except at a cottage or at the main building (bridges or tunnels may occur) and every alley starts and ends at either the main building or one of the cottages.

It is known that at least one cottage has a route (a sequence of alleys, where each alley starts at the vertex where the previous one ended) to the main building. Professor Szu, the well‐known computer science pundit, wishes to travel to the university by a different route every day. Two routes are considered distinct if they differ in at least one alley (note that the order matters and two different alleys connecting the same two vertices are considered distinct).

The university wants to choose a cottage for Professor Szu such that the number of different routes from that cottage to the main building is maximized. If a cottage has more than 36500 different routes, we assume that the professor can live there "forever" (i.e. his number of distinct routes is effectively infinite). In case there is more than one cottage achieving the longest possible habitation time, output all of them.

The problem involves counting the number of routes from a given cottage to the main building. However, because routes are allowed to repeat vertices and alleys, cycles may result in infinitely many routes. In this problem, if the number of different routes exceeds 36500, output the string forever instead of the actual count.

Note: All formulas should be formatted in LaTeX. For example, the threshold is given as \(36500\).

inputFormat

The input is read from the standard input and has the following format:

n m
u1 v1
... 
um vm

Here, n (\(n \ge 1\)) is the number of cottages and m is the number of alleys. The cottages are numbered from 1 to n and the main building is represented by 0. Each of the following m lines contains two integers u and v (with \(u,v \in \{0,1,2,\dots,n\}\)) indicating a one‐way alley from vertex u to vertex v. Note that there can be multiple alleys between two vertices and self-loops are allowed. It is guaranteed that at least one cottage has a route to the main building (vertex 0).

outputFormat

The program should write to the standard output two lines:

  • The first line contains either the maximum number of distinct routes from a cottage to the main building, or the word forever if the number exceeds \(36500\).
  • The second line contains the list of cottages (their indices in increasing order) that achieve this maximum.

Note: When counting, if the number of routes for a given cottage is greater than \(36500\), treat it as infinite and output forever for that cottage.

sample

3 3
1 0
2 1
3 0
1

1 2 3

</p>