#K83537. Code Review Paths
Code Review Paths
Code Review Paths
You are given a directed graph representing code review relationships among developers. Each edge (u, v) indicates that developer u reviews developer v. For a given set of queries, determine whether there exists a sequence of reviews (a path) from one developer to another.
The problem can be modeled as checking reachability in a directed graph. Use breadth-first search (BFS) or depth-first search (DFS) to decide if there is a path between two given nodes.
Note: If there is a cycle in the graph, the search algorithm must avoid infinite loops by keeping track of visited nodes.
The input and output are read from stdin
and written to stdout
respectively.
inputFormat
The input begins with two integers n and r on the first line, where n is the number of developers and r is the number of review relationships. The next r lines each contain two integers u and v representing a directed edge from developer u to developer v (meaning u reviews v). The next line contains an integer q that indicates the number of queries. This is followed by q lines, each with two integers representing a query to check whether there is a path of reviews from the first developer to the second.
outputFormat
For each query, output a single line containing either "YES" if there exists a path from the first developer to the second, or "NO" otherwise.## sample
5 4
1 2
2 3
3 4
4 5
3
1 3
1 4
2 5
YES
YES
YES
</p>