#P3119. [USACO15JAN] Grass Cownoisseur G

    ID: 3947 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>2015USACO拓扑排序强连通分量Tarjan

[USACO15JAN] Grass Cownoisseur G

题目描述

In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-way cow paths all over his farm. The farm consists of N fields, conveniently numbered 1..N, with each one-way cow path connecting a pair of fields. For example, if a path connects from field X to field Y, then cows are allowed to travel from X to Y but not from Y to X.

Bessie the cow, as we all know, enjoys eating grass from as many fields as possible. She always starts in field 1 at the beginning of the day and visits a sequence of fields, returning to field 1 at the end of the day. She tries to maximize the number of distinct fields along her route, since she gets to eat the grass in each one (if she visits a field multiple times, she only eats the grass there once).

As one might imagine, Bessie is not particularly happy about the one-way restriction on FJ's paths, since this will likely reduce the number of distinct fields she can possibly visit along her daily route. She wonders how much grass she will be able to 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 along the route in the wrong direction. Bessie can only travel backwards at most once in her journey. In particular, she cannot even take the same path backwards twice.

输入格式

The first line of input contains N and M, giving the number of fields and the number of one-way paths (1 <= N, M <= 100,000).

The following M lines each describe a one-way cow path. Each line contains two distinct field numbers X and Y, corresponding to a cow path from X to Y. The same cow path will never appear more than once.

输出格式

A single line indicating the maximum number of distinct fields Bessie can visit along a route starting and ending at field 1, given that she can follow at most one path along this route in the wrong direction.

题目大意

题目描述

为了更好地管理牛群的放牧路线,Farmer John 在他的农场中安装了若干单向牛道。农场由 NN 块草场组成,编号为 11NN,每条单向牛道连接一对草场。例如,若存在一条从草场 XXYY 的路径,则牛可以从 XX 前往 YY,但不能从 YY 返回 XX

众所周知,Bessie 喜欢尽可能多地品尝不同草场的牧草。她每天从草场 11 出发,访问一系列草场后返回草场 11。她试图最大化沿途经过的不同草场数量(重复访问的草场只算一次)。

由于单向路径的限制,Bessie 担心这会减少她每日路线中可以访问的草场数量。她想知道如果她违反规则,在路线中最多逆向通过某条道路一次,最多能品尝多少草场的牧草。请计算她从草场 11 出发并返回的情况下,最多能访问的不同草场数量。注意 Bessie 在整个旅程中最多只能逆向通过一条道路,且同一条路径不能逆向两次。

输入格式

第一行包含两个整数 NNMM,表示草场数量和单向牛道数量(1N,M100,0001 \leq N, M \leq 100,000)。

接下来 MM 行每行描述一条单向牛道,包含两个不同的整数 XXYY,表示从 XXYY 的单向路径。保证每条路径不会重复出现。

输出格式

输出一行,表示 Bessie 在最多逆向通过一条道路的情况下,从草场 11 出发并返回时能访问的最大不同草场数量。

说明/提示

样例解析:

以下是样例输入的 ASCII 图示:

v---3-->6
7   | \ |
^\  v  \|
| \ 1   |
|   |   v
|   v   5
4<--2---^

Bessie 可以通过逆向路径 535\to 3 访问草场 1,2,4,7,2,5,3,11, 2, 4, 7, 2, 5, 3, 1。到达草场 33 后,若不再次逆向其他路径则无法前往 66

7 10 
1 2 
3 1 
2 5 
2 4 
3 7 
3 5 
3 6 
6 5 
7 2 
4 7 


6 

提示

SOLUTION NOTES:

Here is an ASCII drawing of the sample input:

v---3-->6
7   | \ |
^\  v  \|
| \ 1   |
|   |   v
|   v   5
4<--2---^

Bessie can visit pastures 1, 2, 4, 7, 2, 5, 3, 1 by traveling backwards on the path between 5 and 3. When she arrives at 3 she cannot reach 6 without following another backwards path.