atcoder#ABC292E. [ABC292E] Transitivity

[ABC292E] Transitivity

Score : 500500 points

Problem Statement

You are given a simple directed graph with NN vertices numbered 11 to NN and MM edges numbered 11 to MM. Edge ii is a directed edge from vertex uiu_i to vertex viv_i.

You may perform the following operation zero or more times.

  • Choose distinct vertices xx and yy such that there is no directed edge from vertex xx to vertex yy, and add a directed edge from vertex xx to vertex yy.

Find the minimum number of times you need to perform the operation to make the graph satisfy the following condition.

  • For every triple of distinct vertices aa, bb, and cc, if there are directed edges from vertex aa to vertex bb and from vertex bb to vertex cc, there is also a directed edge from vertex aa to vertex cc.

Constraints

  • 3N20003 \leq N \leq 2000
  • 0M20000 \leq M \leq 2000
  • 1ui,viN1 \leq u_i ,v_i \leq N
  • uiviu_i \neq v_i
  • (ui,vi)(uj,vj)(u_i,v_i) \neq (u_j,v_j) if iji \neq j.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN MM

u1u_1 v1v_1

\vdots

uMu_M vMv_M

Output

Print the answer.

4 3
2 4
3 1
4 3
3

Initially, the condition is not satisfied because, for instance, for vertices 22, 44, and 33, there are directed edges from vertex 22 to vertex 44 and from vertex 44 to vertex 33, but not from vertex 22 to vertex 33.

You can make the graph satisfy the condition by adding the following three directed edges:

  • one from vertex 22 to vertex 33,
  • one from vertex 22 to vertex 11, and
  • one from vertex 44 to vertex 11.

On the other hand, the condition cannot be satisfied by adding two or fewer edges, so the answer is 33.

292 0
0
5 8
1 2
2 1
1 3
3 1
1 4
4 1
1 5
5 1
12