#abc292e. E - Transitivity

E - Transitivity

Score : 500500 points

问题描述

你给定一个简单的有向图,包含 NN 个顶点,编号为 11NN,以及 MM 条边,编号为 11MM。其中第 ii 条边是从顶点 uiu_i 到顶点 viv_i 的有向边。

你可以执行以下操作零次或多次:

  • 选择两个不同的顶点 xxyy,确保当前不存在从顶点 xx 到顶点 yy 的有向边,然后添加一条从顶点 xx 到顶点 yy 的有向边。

找出需要执行上述操作的最小次数,使得图满足以下条件:

  • 对于每三个不同的顶点 aabbcc,如果存在从顶点 aa 到顶点 bb 以及从顶点 bb 到顶点 cc 的有向边,则也必须存在从顶点 aa 到顶点 cc 的有向边。

以上为通义千问 qwen-max 翻译,仅供参考。

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.

Sample Input 1

4 3
2 4
3 1
4 3

Sample Output 1

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.

Sample Input 2

292 0

Sample Output 2

0

Sample Input 3

5 8
1 2
2 1
1 3
3 1
1 4
4 1
1 5
5 1

Sample Output 3

12

update @ 2024/3/10 12:11:09