PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FW0000122. Truy vấn khả năng đi tới (Reachability Queries)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Truy vấn khả năng đi tới (Reachability Queries)
Reachability Queries
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
Given a directed graph with nodes and edges, possibly containing cycles, answer reachability queries . For each query determine if there exists a directed walk from to . Every node reaches itself by a zero-edge walk.
Input
First line , then directed edges , then queries . Nodes are -indexed.
Output
Print YES or NO for each query, one per line.
Subtasks
-
Subtask 1 (40 points): , , . All input conditions remain in force.
-
Subtask 2 (60 points): , . All input conditions remain in force.
Examples
Input
4 4 3
1 2
2 3
3 1
4 3
1 3
1 4
4 1
Output
YES
NO
YES
Explanation
Nodes 1,2,3 form one strongly connected component; node 4 reaches that component but the reverse direction is impossible.