303555: CF687E. TOF

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

TOF

题目描述

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: `<br></br>dfs(v)<br></br>{<br></br> set count[v] = count[v] + 1<br></br> if(count[v] < 1000)<br></br> {<br></br> foreach u in neighbors[v]<br></br> {<br></br> if(visited is equal to false)<br></br> {<br></br> dfs(u)<br></br> }<br></br> break<br></br> }<br></br> }<br></br> set visited[v] = true<br></br>}<br></br><br></br>main()<br></br>{<br></br> input the digraph()<br></br> TOF()<br></br> foreach 1<=i<=n<br></br> {<br></br> set count[i] = 0 , visited[i] = false<br></br> }<br></br> foreach 1 <= v <= n<br></br> {<br></br> if(visited[v] is equal to false)<br></br> {<br></br> dfs(v)<br></br> }<br></br> }<br></br> ... // And do something cool and magical but we can't tell you what!<br></br>}<br></br>`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.

输入输出格式

输入格式


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 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF687E/7598ba83246bf3fe4f0a4896c607816bd55544b6.png) 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.

输出格式


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

输入输出样例

输入样例 #1

3 3
1 2
2 3
3 1

输出样例 #1

2998

输入样例 #2

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

输出样例 #2

3001

Input

加入题单

上一题 下一题 算法标签: