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 uivi (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或者贪心可以解决此题,但是由于每一次的复杂度都是O(N),而每一次的可用点是不多的,注意到sigma(K)<1e5,显然需要建立虚树,在虚树上做DP或者贪心。

看到很多种虚树的写法,这个比较好记:

1,先按DFS序排序,然后得到相邻的LCA,加入点集,去重。

2,建虚树,维护一个栈,对于当前要加入的点,如果它不在栈顶的点的子树内(也就是代码里的belong,用dfs序很方便的判断),我们就一直退栈,然后栈顶向当前点连边即可。(队列里维护的是一条从根到此点的dep递增的DFS链

得到虚树后就可以DP了:

从下向上遍历,如果当前点是染色的点,那么它需要和染色儿子切断。如果不是染色的点,当染色儿子大于1时,需要去掉此点;当染色儿子等于1时,不用管。

#include<bits/stdc++.h>
const int maxn=;
using namespace std;
int Laxt[maxn],Next[maxn<<],To[maxn<<],dep[maxn],cnt;
int in[maxn],out[maxn],fa[maxn][],times,a[maxn];
int q[maxn],col[maxn],sz[maxn],top,tot,ans;
vector<int>G[maxn];
void add(int u,int v){ Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; }
bool cmp(int x,int y){ return in[x]<in[y]; }
void dfs1(int u,int f)
{
in[u]=++times; fa[u][]=f; dep[u]=dep[f]+;
for(int i=Laxt[u];i;i=Next[i]) if(To[i]!=f) dfs1(To[i],u);
out[u]=times;
}
int LCA(int u,int v)
{
if(dep[u]<dep[v]) swap(u,v);
for(int i=;i>=;i--) if(dep[fa[u][i]]>=dep[v]) u=fa[u][i];
if(u==v) return u;
for(int i=;i>=;i--) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
return fa[u][];
}
bool belong(int x,int y){ return in[x]>=in[y]&&in[x]<=out[y]; } //判断x是否在y的子树里。
bool check()
{
for(int i=;i<=tot;i++)
if(col[fa[a[i]][]]) return false;
return true;
}
void build() //建立虚树
{
top=; for(int i=;i<=tot;i++){
while(top&&!belong(a[i],q[top])) top--;
if(top) G[q[top]].push_back(a[i]);
q[++top]=a[i];
}
}
void dfs2(int u) //DP
{
int L=G[u].size();
for(int i=;i<L;i++){
dfs2(G[u][i]);
sz[u]+=sz[G[u][i]];
}
if(col[u]) ans+=sz[u],sz[u]=;
else if(sz[u]>) ans++,sz[u]=;
}
int main()
{
int N,Q,K,u,v,i,j;
scanf("%d",&N);
for(i=;i<N;i++){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
dfs1(,);
for(i=;i<=;i++)
for(j=;j<=N;j++)
fa[j][i]=fa[fa[j][i-]][i-];
scanf("%d",&Q);
while(Q--){
scanf("%d",&K); tot=K; ans=;
for(i=;i<=K;i++) scanf("%d",&a[i]),col[a[i]]=;
if(!check()){ puts("-1"); for(i=;i<=K;i++) col[a[i]]=; continue; }
sort(a+,a+tot+,cmp);
for(i=;i<=K;i++) a[++tot]=LCA(a[i-],a[i]);
sort(a+,a+tot+,cmp);
tot=unique(a+,a+tot+)-(a+);
build();
dfs2(a[]);
printf("%d\n",ans);
for(i=;i<=tot;i++) col[a[i]]=sz[a[i]]=,G[a[i]].clear();
}
return ;
}

最新文章

  1. C语言 第四章 关系、逻辑运算与分支流程控制
  2. c语言二维数组求最大值
  3. 黑马程序员——C语言基础 函数
  4. android中TimePicker和DatePicker的简单使用
  5. linux下vim命令详解 转自: zhanglong0426
  6. [转]美国的软件公司是什么样?---- 以Fog Creek为例
  7. CMake with Win&amp;MinGW
  8. Oracle自动增长的序列号
  9. 总结QueueUserWorkItem传参的几种方式
  10. 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
  11. SilverlightLoader使用托管代码创建自定义载入界面及动态加载XAP
  12. 每天一个linux命令31)--chown命令
  13. oracle 恢复数据到某个时间点
  14. Spring Boot + Vue 前后端分离,两种文件上传方式总结
  15. python to shell vimdiff
  16. C# 批量新增的两种方法。
  17. kettle 在javascrip代码组件中使用fireToDB()函数实现自定义数据库查询
  18. java项目反编译获得源码
  19. sql server 2008 windows验证改混合登陆中SqlServer身份验证用户名密码
  20. 51单片机学习笔记(郭天祥版)(1)——单片机基础和点亮LED灯

热门文章

  1. scapy windows install
  2. Socket的UDP协议在erlang中的实现
  3. kfaka windows安装
  4. WPF自定义选择年月控件详解
  5. C#使用for循环移除HTML标记
  6. mysql 5.5主从复制配置
  7. C#判断WebService接口是否可用
  8. HDU 5374 Tetris (2015年多校比赛第7场)
  9. 【BZOJ1097】[POI2007]旅游景点atr 最短路+状压DP
  10. 如何利用Windows System Image Manager制作一个answer file