#K11741. Travel Possibility Checker

    ID: 23536 Type: Default 1000ms 256MiB

Travel Possibility Checker

Travel Possibility Checker

This problem involves determining if it is possible to travel between two bus stops in a city using the given bus routes. The stops are numbered from 1 to S, and there are R bidirectional routes connecting different stops. For each query, you must decide whether there exists a path from bus stop a to bus stop b by traversing the available routes.

You can use standard graph traversal algorithms such as breadth-first search (BFS) or depth-first search (DFS) to solve this problem.

Mathematical Insight: Given the graph \(G(V, E)\), where \(V\) represents bus stops and \(E\) the set of edges between stops, you need to determine for each query if there exists a sequence of vertices \(v_1, v_2, \dots, v_k\) with \(v_1 = a\) and \(v_k = b\) such that \(\forall i, (v_i, v_{i+1}) \in E\).

inputFormat

The input is given through stdin and is formatted as follows:

  • The first line contains two integers S and R, representing the number of bus stops and the number of bus routes respectively.
  • The next R lines each contain two integers u and v, denoting a bidirectional bus route between stops u and v.
  • The next line contains an integer Q, the number of queries.
  • The following Q lines each contain two integers a and b, representing a query to determine if travel is possible from stop a to stop b.

outputFormat

For each query, output a single line to stdout containing either YES if it is possible to travel between the two stops, or NO otherwise.

## sample
5 4
1 2
2 3
3 4
4 5
3
1 5
2 4
1 3
YES

YES YES

</p>