QOJ.ac

QOJ

Límite de tiempo: 3 s Límite de memoria: 1024 MB Puntuación total: 100 Hackeable ✓
Estadísticas

Sunghyeon 居住的村庄由 $N$ 个路口和 $M$ 条双向道路组成。路口编号为 $1$ 到 $N$ 的整数。

每条双向道路连接两个不同的路口,且任意两个路口之间都存在一条路径。此外,任意两个路口之间最多只有一条直接相连的道路。

周日,Sunghyeon 离开家去朋友 Changki 家玩,Changki 也住在同一个村庄。由于 Sunghyeon 刚搬来,她对村庄的道路网络一无所知,只能分辨出她自己的家和 Changki 的家:其他所有房子在她看来都一样。在漫无目的地游荡了很久之后,她终于到达了 Changki 的家。

Sunghyeon 记得,出发后她再也没有回到过她家所在的那个路口,并且当她到达 Changki 家所在的路口时,她立即进入了 Changki 的家。

Sunghyeon 想知道,当她去 Changki 家玩时,她可能经过了多少个路口。换句话说,在 $N$ 个路口中,她想统计有多少个路口 $V$,使得存在一条她可能经过 $V$ 的路径。

好奇的 Sunghyeon 更进一步,她想知道如果她的家在路口 $A$,而 Changki 的家在路口 $B$,她可能经过了多少个不同的路口。她想要得到许多不同点对 $(A, B)$ 的答案。

请编写一个程序来帮助 Sunghyeon 和 Changki。

输入格式

第一行包含一个整数 $T$,表示测试用例的数量 ($1 \le T \le 1000$)。接下来是各测试用例。

每个测试用例的第一行包含两个整数 $N$ 和 $M$,分别表示路口数量和道路数量 ($2 \le N \le 200\,000, 1 \le M \le 500\,000$)。

接下来的 $M$ 行,每行包含两个整数 $U_i$ 和 $V_i$,表示这两个路口之间有一条双向道路 ($1 \le U_i, V_i \le N, U_i \neq V_i$)。任意两个路口之间都存在路径。此外,任意两个路口之间最多只有一条直接相连的道路。

下一行包含一个整数 $Q$,表示询问的数量 ($1 \le Q \le 500\,000$)。

接下来的 $Q$ 行,每行包含两个整数 $A_j$ 和 $B_j$,表示你需要求出如果 Sunghyeon 的家在路口 $A_j$,Changki 的家在路口 $B_j$ 时,她可能经过的不同路口的数量 ($1 \le A_j, B_j \le N, A_j \neq B_j$)。

所有测试用例的 $N$ 之和不超过 $200\,000$。所有测试用例的 $M$ 之和不超过 $500\,000$。所有测试用例的 $Q$ 之和不超过 $500\,000$。

输出格式

对于每个测试用例,打印 $Q$ 行。第 $i$ 行必须包含当 Sunghyeon 的家在路口 $A_i$ 且 Changki 的家在路口 $B_i$ 时,她可能经过的不同路口的数量。

样例

输入 1

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

输出 1

2
4
3
3
5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.