QOJ.ac

QOJ

Limite de temps : 3 s Limite de mémoire : 512 MB Points totaux : 100

#2272. 蚂蚁推箱子

Statistiques

最近发现了一种名为 Formica sokobanica 的新蚂蚁。由于其独特的习性,它引起了昆虫学家的注意。这些蚂蚁不形成蚁群。相反,个体构建自己的私人巢穴,并将食物(坚果)储存在巢穴中。巢穴由许多通过隧道连接的小房间组成。它们建造的房间只比坚果稍大一点,留出足够的空间供空气流通;它们无法进入装有坚果的房间。为了节省劳力,隧道非常狭窄,刚好能容纳一个坚果的大小,因此坚果不应留在隧道中,以保持空气流通。

要进入一个装有坚果的房间,必须将坚果推到通过隧道与该房间相连的任何空闲房间中。当除了蚂蚁进来的那个房间外,没有其他相邻房间是空闲的时,坚果就无法被推走,因此蚂蚁无法进入该房间。

昆虫学家之一 Myrmink 博士对这些蚂蚁非常着迷,他绘制了一个典型巢穴的图表。该图表还显示了哪些房间里存有坚果,以及蚂蚁最初所在的房间。你的任务是编写一个程序,计算蚂蚁可以到达并进入的房间数量。将坚果推入其中一个空闲的相邻房间可能会使某些房间变得无法到达,而选择推入另一个房间可能会使这些房间保持可达状态。此类选择可能有多种组合。在这种情况下,应统计所有可以通过一种或多种选择组合到达的房间。

你可以假设蚂蚁最初所在的房间里没有坚果,并且巢穴中没有环路。

输入格式

输入包含一个测试用例,按以下格式表示巢穴的图表:

$n$ $m$ $x_1$ $y_1$ $\vdots$ $x_{n-1}$ $y_{n-1}$ $a_1$ $\vdots$ $a_m$

其中 $n$ 和 $m$ 分别是房间和坚果的数量。它们满足 $1 \le n \le 2 \times 10^5$ 且 $0 \le m < n$。房间编号从 $1$ 到 $n$。蚂蚁最初位于房间 $1$。

接下来的 $n-1$ 行,每行包含两个整数 $x_i$ 和 $y_i$ ($1 \le i \le n-1$),表示一条隧道连接编号为 $x_i$ 和 $y_i$ 的房间。满足 $1 \le x_i \le n$,$1 \le y_i \le n$ 且 $x_i \neq y_i$。没有两条隧道连接同一对房间。

剩余的 $m$ 行,每行包含一个整数 $a_k$ ($1 \le k \le m, 2 \le a_k \le n$),表示编号为 $a_k$ 的房间里有一个坚果。所有 $a_k$ 互不相同。

输出格式

输出应为一行,包含一个整数,即蚂蚁可以到达并进入的房间数量。

样例

样例输入 1

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

样例输出 1

2

样例输入 2

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

样例输出 2

7

样例输入 3

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

样例输出 3

2

样例输入 4

11 3
1 2
2 3
3 4
2 5
5 6
6 7
7 8
6 9
9 10
6 11
2
3
7

样例输出 4

9

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.