Description

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.

Input

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 ui, vi (1 ≤ ui, vi ≤ 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 ki — the number of important cities in the King's plan, (1 ≤ ki ≤ n), then follow exactly ki space-separated pairwise distinct numbers from 1 to n — the numbers of important cities in this plan.

The sum of all ki's does't exceed 100 000.

Output

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.

Examples
Input
4
1 3
2 3
4 3
4
2 1 2
3 2 3 4
3 1 2 4
4 1 2 3 4
Output
1
-1
1
-1
Input
7
1 2
2 3
3 4
1 5
5 6
5 7
1
4 2 4 6 7
Output
2
Note

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.

正解:虚树+DP

解题报告:

  上次考试原题...

  题目要求使得k个点互相不连通的最小代价。

  考虑直接构虚树,然后贪心。具体看我的代码,不再赘述。关键是虚树注意有一些细节不要写错了。

 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
#define RG register
const int MAXN = ;
int n,m,ecnt,id[MAXN],k;
int first[MAXN],next[MAXN*],to[MAXN*];
int jump[MAXN][],deep[MAXN];
int que[MAXN],top,Stack[MAXN];
int head[MAXN];
int ans;
bool in[MAXN];
struct edge{
int to,next;
}e[MAXN];
inline bool cmp(RG int a,RG int b){return id[a]<id[b];}
inline void link(RG int x,RG int y){ if(x==y) return ; e[++ecnt].next=head[x]; head[x]=ecnt; e[ecnt].to=y;}
inline int getint()
{
RG int w=,q=; RG char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline void dfs(RG int x,RG int fa){
jump[x][]=fa; id[x]=++ecnt;
for(int i=;i<=;i++) jump[x][i]=jump[jump[x][i-]][i-];
for(int i=first[x];i;i=next[i]) {
RG int v=to[i]; if(v==fa) continue;
deep[v]=deep[x]+; dfs(v,x);
}
} inline int lca(RG int x,RG int y){
if(deep[x]<deep[y]) swap(x,y);
RG int t=; while((<<t) <= deep[x]) t++;
t--; for(int i=t;i>=;i--) if(deep[x]-(<<i)>=deep[y]) x=jump[x][i];
if(x==y) return y;
for(RG int i=t;i>=;i--) if(jump[x][i]!=jump[y][i]) { x=jump[x][i]; y=jump[y][i]; }
return jump[x][];
} inline int dp(RG int x,RG int fa){
RG int cap=,lin; RG bool flag=false,ff=true;
if(in[x]){//自己是的话,必须把所有有指挥官的儿子结点的路都堵上
cap++;
for(RG int i=head[x];i;i=e[i].next) {
RG int v=e[i].to; if(v==fa) continue;
lin=dp(v,x); cap+=lin;
if(lin>) ans++;
}
}
else{//自己不是的话,如果只有一个儿子结点则不用管,如果超过一个则需要把当前结点摧毁,并且等价于与上面部分断开了
for(RG int i=head[x];i;i=e[i].next) {
RG int v=e[i].to; if(v==fa) continue;
lin=dp(v,x); cap+=lin;
if(lin!= && flag && ff) ans++,ff=false;
if(lin>)flag=true;//!!!!!!只有在儿子结点有未处理的情况下才需要打标记!!!!!!
}
if(!ff) cap=;
}
head[x]=;
return cap;
} inline bool solve(){
m=getint(); for(RG int i=;i<=m;i++) que[i]=getint(),in[que[i]]=;
for(RG int i=;i<=m;i++) for(RG int j=first[que[i]];j;j=next[j]) if(in[to[j]]) return false;
sort(que+,que+m+,cmp);//按dfs序排序
top=;Stack[++top]=; RG int grand;
ecnt=;
for(RG int i=;i<=m;i++) {
grand=lca(Stack[top],que[i]);
while() {
if(deep[Stack[top-]]<=deep[grand]) {
link(grand,Stack[top]); top--;
if(Stack[top]!=grand) Stack[++top]=grand;
break;
}
link(Stack[top-],Stack[top]); top--;
}
if(Stack[top]!=que[i]) Stack[++top]=que[i];
}
top--;
while(top) link(Stack[top],Stack[top+]),top--;
ans=; dp(,);
printf("%d\n",ans);
return true;
} inline void work(){
n=getint(); RG int x,y;
for(RG int i=;i<n;i++) {
x=getint(); y=getint();
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;
next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x;
}
ecnt=; deep[]=; dfs(,);
k=getint(); for(RG int i=;i<=k;i++) { if(!solve()) printf("-1\n"); for(int j=;j<=m;j++) in[que[j]]=; }
} int main()
{
work();
return ;
}

最新文章

  1. Maven打包生成可运行bat/sh脚本文件
  2. 12.创建一个Point类,有成员变量x,y,方法getX(),setX(),还有一个构造方 法初始化x和y。创建类主类A来测试它。
  3. JS的基础类型与引用类型
  4. Leetcode: Count Numbers with Unique Digits
  5. 搭建SpringMVC+MyBatis开发框架四
  6. C# 绘制统计图(柱状图, 折线图, 扇形图)
  7. Long Dominoes(ZOJ 2563状压dp)
  8. 解决C# WinForm 中 VSHOST.EXE 程序不关闭的问题
  9. 远程连接(ssh安装)
  10. MySQL慢日志查询全解析:从参数、配置到分析工具【转】
  11. 直接请求转发(Forward)和间接请求转发(Redirect)两种区别?
  12. TP手册学习第一天
  13. 32位centos6.5 mysql rpm包下载
  14. Andorid之页面布局优化
  15. #254 Check for Palindromes
  16. 当对具体的一条记录进行操作时候 需要传递该记录的id
  17. file标签 - 图片上传前预览 - FileReader &amp; 网络图片转base64和文件流
  18. HotSpot虚拟机对象探秘
  19. git如何删除已经 add 的文件 (如何撤销已放入缓存区文件的修改)
  20. MongoDB集群与LBS应用系列(二)--与Hadoop集成

热门文章

  1. 01Spring_基本jia包的导入andSpring的整体架构and怎么加入日志功能
  2. [转]终于找到全annotation配置springMVC的方法了(事务不失效)
  3. Gruntjs: grunt-contrib-jst
  4. [JAVA教程] 2016年最新spring4框架搭建视频教程 【尚学堂】
  5. Asp.net WebApi Put模式调用,“HTTP 错误 405.0 - Method Not Allowed”解决方法
  6. 单片机C语言探究--为什么变量最好要赋初值
  7. MATLAB调用C程序、调试和LDPC译码
  8. JS之获取属性总结
  9. centos hadoop搭建准备
  10. Scala入门之函数进阶