#P4320. 道路相遇

    ID: 3185 远端评测题 2000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>图论倍增O2优化树链剖分,树剖圆方树

道路相遇

Output Format

Output qq lines, each containing a single positive integer.

5 6
1 2
1 3
2 3
3 4
4 5
3 5
1
1 5
3

Hint

From city 11 to city 55 there are 44 possible routes:

123451 \to 2 \to 3 \to 4 \to 5

12351 \to 2 \to 3 \to 5

13451 \to 3 \to 4 \to 5

1351 \to 3 \to 5

It can be seen that Xiao w will always pass through cities 1,3,51, 3, 5, so the answer is 33.

You may assume Xiao w will not visit the same city twice. Of course, even if you think revisiting cities is allowed, it does not affect the answer.

Subtask 1: 15 points, n=5,q=50n = 5, q = 50.

Subtask 2: 15 points, n=100,q=5000n = 100, q = 5000.

Subtask 3: 20 points, n=3000,q=5×105n = 3000, q = 5 \times 10^5.

Subtask 4: 20 points, n=499999,q=5×105,m=n1n = 499999, q = 5 \times 10^5, m = n - 1.

Subtask 5: 30 points, n=q=5×105n = q = 5 \times 10^5.

Constraints: For all testdata: $1 \leq n \leq 5 \times 10^5, 1 \leq q \leq 5 \times 10^5, 1 \leq m \leq \min(\frac{n(n-1)}{2}, 10^6)$.

Translated by ChatGPT 5