QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 32 MB Total points: 100

#11849. 路障

Statistics

Byteland 是一个岛屿,岛上有若干城市,城市之间由双向道路连接。道路网络的设计使得任意两个城市之间有且仅有一条路径(不走回头路)。

不幸的是,困难时期已经到来——Byteland 正在为战争做准备。Byteland 的首席战略家正在制定一份防御计划,其中考虑建立一个“特殊安全区”。该区域将通过封锁 Byteland 现有的部分道路来创建,使得这些道路无法通行。为了使该区域完全安全,必须满足以下规则:

  • 区域内的每个城市都可以到达该区域内的任何其他城市,
  • 无法从区域外的城市到达区域内的城市,
  • 区域必须恰好包含 $k$ 个城市。

目前正在考虑该问题的许多不同解决方案——对于不同的 $k$ 值,需要确定最少需要封锁多少条道路,才能获得一个大小为 $k$(恰好包含 $k$ 个城市)的特殊安全区。请帮助 Byteland 的首席战略家——编写一个程序,针对指定的 $k$ 值计算所需的封锁道路数量。

编写一个程序,完成以下任务:

  • 从标准输入读取 Byteland 的道路描述以及一组查询(不同的 $k$ 值),
  • 对于每个查询,程序应确定构建一个所需大小的“特殊安全区”所需的最少封锁道路数量,
  • 将结果写入标准输出。

输入格式

标准输入的第一行包含一个整数 $n$ ($1 \le n \le 3\,000$),表示 Byteland 的城市数量。城市编号为 $1, 2, \ldots, n$。

接下来的 $n - 1$ 行,每行包含一对整数 $a, b$ ($1 \le a, b \le n$),中间用空格隔开。每一对 $a, b$ 表示连接城市 $a$ 和 $b$ 的一条直接道路。每对城市之间最多只有一条直接道路。

标准输入的下一行包含一个整数 $m$ ($1 \le m \le n$),表示需要处理的查询数量。接下来的 $m$ 行,每行包含一个整数 $k_i$ ($k_i \in \{1, 2, 3, \ldots, n\}$)。它表示第 $i$ 个查询,即特殊安全区内必须包含的城市数量。

输出格式

你的程序应向标准输出写入恰好 $m$ 个数字,每个数字占一行。第 $i$ 行的数字应为:

  • -1,如果无法创建大小为 $k_i$ 的特殊安全区,
  • 否则,为构建大小为 $k_i$ 的特殊安全区所需封锁的最少道路数量。

样例

输入 1

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

输出 1

2
1

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.