PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FW0000046. Tính bắc cầu (Transitivity)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tính bắc cầu (Transitivity)
Transitivity
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Add as few directed edges as necessary so that for all distinct vertices , edges and imply the edge .
Input
Integers , followed by directed edges .
Output
Print the minimum number of edges to add.
Subtasks
- Subtask 1 (40 points): ; , ; distinct directed non-loop edges.
- Subtask 2 (60 points): , ; distinct directed non-loop edges.
Examples
Input
4 3
2 4
3 1
4 3
Output
3
Explanation
The initial edges imply new reachability links 2 to 3, 4 to 1, and 2 to 1. All three must be added, producing the minimum answer 3.