#abc292e. E - Transitivity
E - Transitivity
Score : points
问题描述
你给定一个简单的有向图,包含 个顶点,编号为 到 ,以及 条边,编号为 到 。其中第 条边是从顶点 到顶点 的有向边。
你可以执行以下操作零次或多次:
- 选择两个不同的顶点 和 ,确保当前不存在从顶点 到顶点 的有向边,然后添加一条从顶点 到顶点 的有向边。
找出需要执行上述操作的最小次数,使得图满足以下条件:
- 对于每三个不同的顶点 、 和 ,如果存在从顶点 到顶点 以及从顶点 到顶点 的有向边,则也必须存在从顶点 到顶点 的有向边。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given a simple directed graph with vertices numbered to and edges numbered to . Edge is a directed edge from vertex to vertex .
You may perform the following operation zero or more times.
- Choose distinct vertices and such that there is no directed edge from vertex to vertex , and add a directed edge from vertex to vertex .
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 , , and , if there are directed edges from vertex to vertex and from vertex to vertex , there is also a directed edge from vertex to vertex .
Constraints
- if .
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
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 , , and , there are directed edges from vertex to vertex and from vertex to vertex , but not from vertex to vertex .
You can make the graph satisfy the condition by adding the following three directed edges:
- one from vertex to vertex ,
- one from vertex to vertex , and
- one from vertex to vertex .
On the other hand, the condition cannot be satisfied by adding two or fewer edges, so the answer is .
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