Codeforces Round 360 (Div. 1)
E
TOF
Codeforces
2900
原题
← D→
翻译中…

Today Pari gave Arya a cool graph problem. Arya wrote a non-optimal solution for it, because he believes in his ability to optimize non-optimal solutions. In addition to being non-optimal, his code was buggy and he tried a lot to optimize it, so the code also became dirty! He keeps getting Time Limit Exceeds and he is disappointed. Suddenly a bright idea came to his mind!

翻译中…

Here is how his dirty code looks like:

翻译中…

dfs(v)
{
     set count[v] = count[v] + 1
     if(count[v] < 1000)
     {
          foreach u in neighbors[v]
          {
               if(visited[u] is equal to false)
               {
                    dfs(u)
               }
               break
          }
     }
     set visited[v] = true
}

main()
{
     input the digraph()
     TOF()
     foreach 1<=i<=n
     {
          set count[i] = 0 , visited[i] = false
     }
     foreach 1 <= v <= n
     {
          if(visited[v] is equal to false)
          {
               dfs(v)
          }
     }
     ... // And do something cool and magical but we can't tell you what!
}

翻译中…

He asks you to write the TOF function in order to optimize the running time of the code with minimizing the number of calls of the dfs function. The input is a directed graph and in the TOF function you have to rearrange the edges of the graph in the list neighbors for each vertex. The number of calls of dfs function depends on the arrangement of neighbors of each vertex.

翻译中…

Input

翻译中…

The first line of the input contains two integers $*n*$ and $*m*$ ($1 ≤ *n*, *m* ≤ 5000$) — the number of vertices and then number of directed edges in the input graph.

翻译中…

Each of the next $*m*$ lines contains a pair of integers $*u*_{*i*}$ and $*v*_{*i*}$ ($1  ≤  *u*_{*i*},  *v*_{*i*}  ≤  *n*$), meaning there is a directed image edge in the input graph.

翻译中…

You may assume that the graph won't contain any self-loops and there is at most one edge between any unordered pair of vertices.

翻译中…

Output

翻译中…

Print a single integer — the minimum possible number of dfs calls that can be achieved with permuting the edges.

翻译中…

样例

Input #1
3 3
1 2
2 3
3 1
Output #1
2998
InputCopy OutputCopy
Input #2
6 7
1 2
2 3
3 1
3 4
4 5
5 6
6 4
Output #2
3001
InputCopy OutputCopy
题目信息
题号
687E
时间限制
1 s
内存限制
256 MB
标签
DFS 及类似
图论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。