PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FW0000121. Đếm đỉnh có thể đến (Reachable Nodes)
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 đỉnh có thể đến (Reachable Nodes)
Reachable Nodes
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
Given a directed acyclic graph with vertices, count for every vertex how many distinct vertices are reachable from , including itself via a zero-edge walk. A destination is counted once even when multiple paths lead to it.
Input
First line , followed by directed edges , . Vertices are -indexed.
Output
Print integers in vertex order.
Subtasks
-
Subtask 1 (40 points): , . All input conditions remain in force.
-
Subtask 2 (60 points): , . All input conditions remain in force.
Examples
Input
5 6
1 2
1 3
1 4
2 3
3 5
4 5
Output
5 3 2 2 1
Explanation
Node 1 reaches all five nodes. Nodes 2,3,4,5 respectively reach 3,2,2,1 nodes.