#P1551. Relatives Connectivity

    ID: 14837 Type: Default 1000ms 256MiB

Relatives Connectivity

Relatives Connectivity

This problem is based on the following rule: If \(x\) and \(y\) are relatives and \(y\) and \(z\) are relatives, then \(x\) and \(z\) are also relatives. Moreover, if \(x\) and \(y\) are relatives, then all relatives of \(x\) are also relatives of \(y\) and vice versa. Given this transitive relationship, your task is to determine whether two individuals belong to the same family group.

You are given \(N\) persons and \(M\) pairs of people who are directly known to be relatives. Then, you are given \(Q\) queries, each asking whether a pair of persons are relatives (either directly or indirectly).

Note: Persons are numbered from 1 to \(N\).

inputFormat

The first line contains two integers \(N\) and \(M\) \((1 \leq N \leq 10^5, 0 \leq M \leq 10^5)\) representing the number of persons and the number of known relative pairs respectively.

The next \(M\) lines each contain two integers \(u\) and \(v\) indicating that person \(u\) and person \(v\) are relatives.

The following line contains an integer \(Q\) \((1 \leq Q \leq 10^5)\), the number of queries.

Each of the next \(Q\) lines contains two integers \(a\) and \(b\). For each query, you need to check whether person \(a\) and person \(b\) are relatives.

outputFormat

For each query, print "Yes" if the two persons are relatives; otherwise, print "No". Each answer should be printed on a new line.

sample

4 2
1 2
3 4
3
1 2
2 3
3 4
Yes

No Yes

</p>