#C7397. Communicating Servers
Communicating Servers
Communicating Servers
You are given n servers and a list of m direct links connecting pairs of them. Two servers can communicate if there exists a series of direct links connecting them. For each of the q queries, determine whether a given pair of servers can communicate.
The connectivity can be expressed mathematically using the Union-Find data structure, where the condition for two servers a and b to communicate is given by:
\(find(a) = find(b)\)
Answer each query by printing YES
if the servers can communicate and NO
otherwise.
inputFormat
The input is read from stdin and has the following format:
- The first line contains two integers n and m, where n is the number of servers and m is the number of direct links.
- The next m lines each contain two integers u and v, representing a direct link between servers u and v (0-indexed).
- The following line contains an integer q, the number of queries.
- Each of the next q lines contains two integers a and b, representing a query asking whether server a can communicate with server b.
outputFormat
For each of the q queries, output a single line with either YES
if server a and server b can communicate, or NO
if they cannot.## sample
6 5
0 1
0 2
1 3
1 4
4 5
3
3 5
2 5
0 4
YES
YES
YES
</p>