#C2118. Delivery Request Fulfillment

    ID: 45399 Type: Default 1000ms 256MiB

Delivery Request Fulfillment

Delivery Request Fulfillment

You are given a network of zones and direct roads connecting some of them. Each road connects two zones bidirectionally. Given several delivery requests, each specified as a pair of zones, your task is to determine whether the delivery request can be fulfilled. A request can be fulfilled if and only if the two zones are in the same connected component of the network.

The connectivity can be expressed mathematically using graph theory. Let \(G=(V,E)\) be an undirected graph where \(V = \{1,2,\dots,n\}\) and each edge \(e\in E\) connects two vertices \(u\) and \(v\). For any two zones \(a\) and \(b\), a delivery request is fulfilled if \(a\) and \(b\) belong to the same connected component of \(G\).

Use an efficient algorithm such as Depth First Search (DFS) or Union-Find to solve the problem for large inputs.

inputFormat

The input is read from standard input (stdin).

The first line contains two integers (n) and (m), where (n) is the number of zones and (m) is the number of direct roads.

The next (m) lines each contain two integers (u) and (v) indicating that there is a bidirectional road between zone (u) and zone (v).

The following line contains an integer (q), the number of delivery requests.

Then, (q) lines follow, each containing two integers (a) and (b) representing a delivery request from zone (a) to zone (b).

outputFormat

For each delivery request, output a single line containing "YES" if the request can be fulfilled (i.e., both zones are connected), or "NO" otherwise. The output should be written to standard output (stdout).## sample

5 4
1 2
2 3
3 4
4 5
3
1 5
1 3
5 2
YES

YES YES

</p>