Farmer John has installed one-way cow paths all over his farm. The farm consists of N fields, numbered 1..N, with each one-way cow path connecting a pair of fields (a path from X to Y allows travel from X to Y but not Y to X).
Bessie always starts in field 1, visits a sequence of fields, and returns to field 1 at the end of the day. She tries to maximize the number of distinct fields along her route (visiting a field multiple times only counts once).
She wonders how much grass she could eat if she breaks the rules and follows up to one path in the wrong direction. Please compute the maximum number of distinct fields she can visit along a route starting and ending at field 1, where she can follow up to one path in the wrong direction. She cannot take the same path backwards twice.
Input
Line 1: N and M, the number of fields and one-way paths (1 <= N, M <= 100,000).
The following M lines each contain two distinct field numbers X and Y, a cow path from X to Y. The same cow path will never appear more than once.
Output
A single line giving the maximum number of distinct fields Bessie can visit along a route starting and ending at field 1, following at most one path in the wrong direction.
Example 1
7 10 1 2 3 1 2 5 2 4 3 7 3 5 3 6 6 5 7 2 4 7
6
Output details: Bessie can visit 1, 2, 4, 7, 2, 5, 3, 1 by traveling backwards on the path between 5 and 3.
Discussion
Threads about this problem. Spoilers are collapsed; wrap hints in
||double bars||.No discussion yet. Start a thread if something here needs explaining.