QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 256 MB Puntuación total: 140

#13676. 定向

Estadísticas

我们得到一棵拥有 $N$ 个节点的树,节点用 $1$ 到 $N$ 的不同正整数表示。

此外,还给出了树上的 $M$ 个节点对,形式为 $(a_1, b_1), (a_2, b_2), \dots, (a_M, b_M)$。

我们需要为树上的每条边定向,使得对于每个给定的节点对 $(a_i, b_i)$,都存在一条从 $a_i$ 到 $b_i$ 的路径,或者存在一条从 $b_i$ 到 $a_i$ 的路径。问有多少种不同的定向方法可以实现这一目标?

由于答案可能非常大,请输出其模 $10^9 + 7$ 的结果。

输入格式

输入的第一行包含两个正整数 $N$ 和 $M$ ($1 \le N, M \le 3 \cdot 10^5$),分别表示树中的节点数和给定的节点对数。

接下来的 $N - 1$ 行,每行包含两个正整数,表示由一条边相连的两个节点的编号。

再接下来的 $M$ 行中,第 $i$ 行包含两个不同的正整数 $a_i$ 和 $b_i$,表示第 $i$ 个节点对的两个节点编号。所有给定的节点对都是互不相同的。

输出格式

输出单行,包含满足任务要求的给树边定向的不同方法总数,模 $10^9 + 7$ 的结果。

子任务

在占总分 20% 的测试数据中,给定的树是一条链。换句话说,对于所有 $i < N$,节点 $i$ 与节点 $i + 1$ 之间有一条边相连。

在另外占总分 40% 的测试数据中,满足 $N, M \le 5 \cdot 10^3$。

样例

输入样例 1

4 1
1 2
2 3
3 4
2 4

输出样例 1

4

输入样例 2

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

输出样例 2

8

输入样例 3

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

输出样例 3

0

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.