#C8431. Chamber Connectivity with Tunnels

    ID: 52413 Type: Default 1000ms 256MiB

Chamber Connectivity with Tunnels

Chamber Connectivity with Tunnels

You are given a collection of chambers connected by tunnels. Each tunnel connects two distinct chambers. Two chambers are considered connected if there exists a path through one or more tunnels between them. Formally, chambers u and v are connected if there exists a sequence of chambers \( u = x_0, x_1, \ldots, x_k = v \) such that each consecutive pair \( (x_i, x_{i+1}) \) is connected by a tunnel.

Your task is to answer several queries. In each query, you will be given two chambers, and you must determine whether these two chambers are connected.

inputFormat

The input is given via standard input. The first line contains two integers \( n \) and \( m \), where \( n \) is the number of chambers (numbered from 1 to \( n \)) and \( m \) is the number of tunnels.

The next \( m \) lines each contain two space-separated integers \( u \) and \( v \), representing a tunnel between chamber \( u \) and chamber \( v \).

The following line contains an integer \( q \) denoting the number of queries.

Each of the next \( q \) lines contains two integers \( a \) and \( b \). For each query, determine whether chamber \( a \) is connected to chamber \( b \).

outputFormat

For each query, output a single line containing YES if the two chambers are connected, or NO otherwise.

## sample
3 2
1 2
2 3
3
1 2
1 3
2 3
YES

YES YES

</p>