You are given a graph with 'N' nodes numbered 1 to 'N', and 'E' edges, and you have to solve 'M' queries. Each query consists of two integers 'S' and 'T'. Your task is to find the count of nodes that are at a distance of 'T' from the node 'S'.
Two nodes that are directly connected with each other are considered to be at a distance of 1 unit. While two nodes having a common contact without having direct connectivity, are considered to be at a distance of 2 units. In a similar manner, we can calculate distances between nodes.
Note:
The graph may contain cycles.
The first line of each test case contains two space-separated integers 'N' and 'E', the number of nodes and edges in the graph respectively.
The following 'E' lines contain two space-separated integers 'U' and 'V', denoting an undirected edge between the nodes 'U' and 'V'.
The next line contains an integer, 'M', denoting the number of queries.
Then 'M' lines follow:
Each line consists of two integers 'S' and 'T', where 'S' denotes the source node, and 'T' denotes the connectivity distance to be processed.
Output Format:
For each query, print a single line containing a single integer denoting the count of nodes as asked in the problem statement.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= N <= 3 * 10 ^ 3
1 <= E <= 10 ^ 4
1 <= U, V <= N
1 <= M <= 100
1 <= S <= N
1 <= T <= 2500
Time Limit: 1 sec.
4 3
1 2
2 3
3 4
2
3 1
3 2
2
1

The first query asks us to find the number of nodes at a distance of 1 from node 3. Clearly, node 2 and node 4 are at a distance of 1 from node 3. Hence we get 2 as our answer.
The second query asks us to find the number of nodes at a distance of 2 from node 3. Clearly, only node, i.e node 1 is at a distance of 2 from node 3. Hence we get 1 as our answer.
5 6
1 2
1 3
1 5
2 4
3 5
4 5
2
4 2
5 1
2
3

The first query asks us to find the number of nodes at a distance of 2 from node 4. Clearly, node 1 and node 4 are at a distance 2 from node 4. Hence we get 2 as our answer.
The second query asks us to find the number of nodes at a distance of 1 from node 5. Clearly, node 3, node 1, and node 4 are at a distance of 1 from node 5. Hence we get 3 as our answer.
Think how will you traverse the graph.
It’s clearly visible that we need to traverse the graph optimally in order to get our answer.We will use Breadth First Search to achieve this. Let’s understand how to do this.
O(M * (N + E)), where ‘N’ is the number of nodes and ‘E’ is the number of edges and ‘M’ is the number of queries.
Since in the worst case, every node and every edge will be explored. Hence, N + E operations will take place. Hence for M queries it will take M*(N + E) operations. Thus, the complexity will be O(M * (N + E)).
O(N + E), where ‘N’ is the number of nodes and ‘E’ is the number of edges.
Since we will be storing ‘N’ nodes and these ‘N' nodes will be connected using ‘E’ edges. Hence a total of N + E space will be used.