CF613D.Kingdom and its Cities
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daughter's marriage, reforms must be finished as soon as possible.
The kingdom currently consists of n cities. Cities are connected by n - 1 bidirectional road, such that one can get from any city to any other city. As the King had to save a lot, there is only one path between any two cities.
What is the point of the reform? The key ministries of the state should be relocated to distinct cities (we call such cities important). However, due to the fact that there is a high risk of an attack by barbarians it must be done carefully. The King has made several plans, each of which is described by a set of important cities, and now wonders what is the best plan.
Barbarians can capture some of the cities that are not important (the important ones will have enough protection for sure), after that the captured city becomes impassable. In particular, an interesting feature of the plan is the minimum number of cities that the barbarians need to capture in order to make all the important cities isolated, that is, from all important cities it would be impossible to reach any other important city.
Help the King to calculate this characteristic for each of his plan.
与此同时,K王国正在为其国王的女儿筹备婚礼。然而,为了不在亲戚面前失面子,国王必须首先完成王国的改革。由于国王等不及女儿的婚礼,改革必须尽快完成。
目前,该王国由 n 座城市组成。城市之间由 n−1 条双向道路连接,使得任意两座城市之间均可相互到达。由于国王为节省开支,任意两座城市之间仅有唯一一条路径。
改革的意义何在?国家的关键部门需被迁至若干互不相同的城市(我们称这些城市为重要城市)。然而,由于存在蛮族袭击的高风险,这一迁移工作必须谨慎进行。国王已制定了若干方案,每个方案均由一组重要城市描述,现在他想知道哪一个方案最优。
蛮族可以攻占一些非重要城市(重要城市将获得充分防护,因此必然不会被攻占),攻占后这些城市将变得不可通行。特别地,一个方案的有趣性质是:蛮族为使所有重要城市彼此完全隔离(即从任一重要城市均无法到达其他任何重要城市)所需攻占的最少城市数量。
请帮助国王计算其每个方案的这一指标。
输入格式
The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cities in the kingdom.
Each of the next n - 1 lines contains two distinct integers u__i, v__i (1 ≤ u__i, v__i ≤ n) — the indices of the cities connected by the i-th road. It is guaranteed that you can get from any city to any other one moving only along the existing roads.
The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of King's plans.
Each of the next q lines looks as follows: first goes number k__i — the number of important cities in the King's plan, (1 ≤ k__i ≤ n), then follow exactly k__i space-separated pairwise distinct numbers from 1 to n — the numbers of important cities in this plan.
The sum of all k__i's does't exceed 100 000.
输入的第一行包含一个整数 n(1≤n≤100000)—— 表示王国中城市的数量。
接下来的 n−1 行,每行包含两个不同的整数 ui、vi(1≤ui,vi≤n)—— 表示第 i 条道路所连接的两座城市的编号。保证仅通过现有道路即可从任意一座城市到达其他任意一座城市。
下一行包含一个整数 q(1≤q≤100000)—— 表示国王制定的计划数量。
接下来的 q 行,每行格式如下:首先是一个整数 ki —— 表示该计划中重要城市的数量(1≤ki≤n),随后是恰好 ki 个用空格分隔的、互不相同的整数(取值范围为 1 到 n)—— 表示该计划中所有重要城市的编号。
所有 ki 的总和不超过 100000。
输出格式
For each plan print a single integer — the minimum number of cities that the barbarians need to capture, or print - 1 if all the barbarians' attempts to isolate important cities will not be effective.
对于每个计划,输出一个整数——野蛮人需要攻占的最少城市数量;如果所有野蛮人孤立重要城市的尝试均无效,则输出 -1。
输入输出样例
输入#1
4 1 3 2 3 4 3 4 2 1 2 3 2 3 4 3 1 2 4 4 1 2 3 4
输出#1
1 -1 1 -1
输入#2
7 1 2 2 3 3 4 1 5 5 6 5 7 1 4 2 4 6 7
输出#2
2
说明/提示
In the first sample, in the first and the third King's plan barbarians can capture the city 3, and that will be enough. In the second and the fourth plans all their attempts will not be effective.
In the second sample the cities to capture are 3 and 5.
在第一个样例中,按照国王的第一和第三个计划,野蛮人可以攻占城市 3,这就足够了。而在第二和第四个计划中,他们的所有尝试都将无效。
在第二个样例中,需要攻占的城市是 3 和 5。
输入解题思路,AI测评打分。不知道怎么写?