atcoder#ABC254E. [ABC254E] Small d and k

[ABC254E] Small d and k

Score : 500500 points

Problem Statement

We have a simple undirected graph with NN vertices and MM edges. The vertices are numbered 1,,N1,\ldots,N. For each i=1,,Mi=1,\ldots,M, the ii-th edge connects Vertex aia_i and Vertex bib_i. Additionally, the degree of each vertex is at most 3.

For each i=1,,Qi=1,\ldots,Q, answer the following query.

  • Find the sum of indices of vertices whose distances from Vertex xix_i are at most kik_i.

Constraints

  • 1N1.5×1051 \leq N \leq 1.5 \times 10^5
  • 0Mmin(N(N1)2,3N2)0 \leq M \leq \min (\frac{N(N-1)}{2},\frac{3N}{2})
  • 1ai<biN1 \leq a_i \lt b_i \leq N
  • (ai,bi)(aj,bj)(a_i,b_i) \neq (a_j,b_j), if iji\neq j.
  • The degree of each vertex in the graph is at most 33.
  • 1Q1.5×1051 \leq Q \leq 1.5 \times 10^5
  • 1xiN1 \leq x_i \leq N
  • 0ki30 \leq k_i \leq 3
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

a1a_1 b1b_1

\vdots

aMa_M bMb_M

QQ

x1x_1 k1k_1

\vdots

xQx_Q kQk_Q

Output

Print QQ lines. The ii-th line should contain the answer to the ii-th query.

6 5
2 3
3 4
3 5
5 6
2 6
7
1 1
2 2
2 0
2 3
4 1
6 0
4 3
1
20
2
20
7
6
20

For the 11-st query, the only vertex whose distance from Vertex 11 is at most 11 is Vertex 11, so the answer is 11. For the 22-nd query, the vertices whose distances from Vertex 22 are at most 22 are Vertex 22, 33, 44, 55, and 66, so the answer is their sum, 2020. The 33-rd and subsequent queries can be answered similarly.