#C4039. Network Queries in Directed Graphs
Network Queries in Directed Graphs
Network Queries in Directed Graphs
You are given a directed graph with n nodes (numbered from 1 to n). Initially, the graph is empty. You are then given q queries which modify the graph or ask questions about its connectivity.
Each query is one of the following four types:
- A u v: Add a directed edge from node u to node v.
- R u v: Remove the directed edge from node u to node v (if it exists).
- D u v: Check if there is a direct edge from node u to node v. Output
YES
if it exists, orNO
otherwise. - P u v: Check if there is a path (possibly through intermediate nodes) from node u to node v. Output
YES
if such a path exists, orNO
otherwise.
The queries of type A
and R
modify the graph but do not require an output. The queries of type D
and P
require you to print an answer.
Note: When checking for a path (query type P
), you may use any method (such as DFS or BFS). If a query asks for a direct connection (query type D
), only check if the edge from u to v exists immediately.
Input and Output are handled through standard input and standard output.
inputFormat
The input is read from standard input. The first line contains two integers n and q separated by a space, where n is the number of nodes and q is the number of queries.
Each of the next q lines contains a query in one of the following formats:
A u v
R u v
D u v
P u v
Here, u and v are integers representing node numbers.
outputFormat
For each query of type D
or P
, output a single line containing either YES
or NO
. The answers must be printed in the order the queries appear.
5 7
A 1 2
A 2 3
D 1 2
P 1 3
R 1 2
D 1 2
P 1 3
YES
YES
NO
NO
</p>