PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FW0000064. Đếm số hành trình (Numbering Paths)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đếm số hành trình (Numbering Paths)
Numbering Paths
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
Count the number of directed walks of positive length from every vertex i to every vertex j. Walks may repeat vertices and edges. Output -1 for a pair if infinitely many walks exist.
Input
First n m; then m directed edges u v (0-based, no loops or duplicates).
Output
Print an n by n matrix. Entry (i,j) is the number of positive-length directed walks, or -1 if infinitely many exist.
Subtasks
- Subtask 1 (40 points): . 0 <= u,v < n, u != v. Edges are directed and there are no duplicate ordered pairs.
- Subtask 2 (60 points): . 0 <= u,v < n, u != v. Edges are directed and there are no duplicate ordered pairs.
Examples
Input
4 4
0 1
1 2
2 1
0 3
Output
0 -1 -1 1
0 -1 -1 0
0 -1 -1 0
0 0 0 0
Explanation
Edges 1→2 and 2→1 form a cycle, creating infinitely many walks from 0 to 1 or 2. Edge 0→3 provides exactly one walk from 0 to 3. Empty diagonal walks are not counted.