#C437. Friend Groups Connectivity Query
Friend Groups Connectivity Query
Friend Groups Connectivity Query
You are given an undirected graph with n nodes and m edges. Each edge connects two nodes. Your task is to answer q queries. In each query, you are provided with two nodes, and you have to determine whether there exists a path connecting these two nodes.
The graph may consist of several disconnected components. Formally, given a graph \(G = (V, E)\), where \(V\) is the set of nodes with \(|V| = n\) and \(E\) is the set of edges, and given queries \((u, v)\), determine if \(u\) and \(v\) belong to the same connected component.
Input/Output: The input is provided via standard input (stdin) and the output must be printed to standard output (stdout).
inputFormat
The first line contains two integers \(n\) and \(m\) separated by a space, representing the number of nodes and the number of edges respectively. The next \(m\) lines each contain two integers \(u\) and \(v\) indicating that there is an undirected edge connecting node \(u\) and node \(v\). Following that, there is a line containing a single integer \(q\) representing the number of queries. Each of the next \(q\) lines contains two integers \(a\) and \(b\) representing a query to check if there exists a path between node \(a\) and node \(b\).
Note: Nodes are numbered from 0 to \(n-1\).
outputFormat
For each query, output either True
or False
(each on a new line) depending on whether there is a path between the given nodes.
5 3
0 1
1 2
3 4
2
0 2
0 4
True
False
</p>