QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 1024 MB Total points: 100

#4814. Exciting Travel

统计

在结束了 IODS 团队选拔后,你想要去一个新的国家旅行放松。这个国家包含 $n$ 个城市,由 $n-1$ 条双向道路连接。任意两个城市之间存在唯一的简单路径。

在接下来的 $m$ 天里,你希望探索这个国家。每天你都设定了 $k$ 个城市 $x_1, x_2, \dots, x_k$,希望按顺序访问它们。每天开始时,你可以选择任意城市作为旅行的起点,然后你需要依次到达城市 $x_1$,接着是城市 $x_2$,……,最后到达城市 $x_k$ 以完成当天的旅行。

为了增加旅行的乐趣,你不希望在一天内多次经过同一个城市。在任何时刻,你可以选择沿着道路从当前城市走到另一个城市,或者选择乘坐游艇前往任意城市。

你想要知道,对于每一天的旅行,为了避免多次经过同一个城市,最少需要乘坐多少次游艇。注意,每一天的旅行都是独立的:前一天经过的城市在第二天仍然可以经过。

输入格式

第一行包含两个整数 $n$ 和 $m$ ($1 \le n \le 2 \cdot 10^5$, $0 \le m \le 5 \cdot 10^4$)。

接下来的 $n-1$ 行,每行包含两个整数 $x$ 和 $y$ ($1 \le x, y \le n, x \neq y$),表示顶点 $x$ 和 $y$ 之间有一条双向边。保证给定的图是连通的。

接下来的 $m$ 行,每行描述一个查询,格式为 $k \ x_1 \ x_2 \dots x_k$。保证对于所有 $1 \le i < j \le k$,都有 $1 \le x_i \le n$ 且 $x_i \neq x_j$。

单次测试中 $k$ 的总和不超过 $2 \cdot 10^5$。

输出格式

对于每一天的旅行,输出一行,包含一个整数:最少需要的游艇乘坐次数。

样例

输入 1

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

输出 1

1
1
2

输入 2

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

输出 2

0
0
0
1
4
3
5

说明

problem_4814_1.png

该图对应第一个样例测试用例

problem_4814_2.png

该图对应第二个样例测试用例

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.