PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FW0000090. Đường ngắn sau khi thêm cung (Shortest Distance After Road Addition Queries I)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đường ngắn sau khi thêm cung (Shortest Distance After Road Addition Queries I)
Shortest Distance After Road Addition Queries I
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given cities with initially directed roads , each query adds a forward road where . After each query output the shortest path length in number of edges from city to .
Input
First , followed by forward road additions $u v` satisfying .
Output
Print one shortest path length per query.
Subtasks
-
Subtask 1 (40 points): . , có thể thêm một cung đã tồn tại; tất cả các cung đều hướng tới chỉ số lớn hơn.
-
Subtask 2 (60 points): . , có thể thêm một cung đã tồn tại; tất cả các cung đều hướng tới chỉ số lớn hơn.
Examples
Input
5 3
0 3
1 4
0 4
Output
2
2
1
Explanation
Adding 0→3 yields the two-edge route 0→3→4; adding 1→4 does not improve it; adding 0→4 gives a one-edge path.