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 ;
}

CodeForces - 613D:Kingdom and its Cities(虚树+DP)的更多相关文章

  1. 【CF613D】Kingdom and its Cities 虚树+树形DP

    [CF613D]Kingdom and its Cities 题意:给你一棵树,每次询问给出k个关键点,问做多干掉多少个非关键点才能使得所有关键点两两不连通. $n,\sum k\le 10^5$ 题 ...

  2. CF613D Kingdom and its Cities 虚树 树形dp 贪心

    LINK:Kingdom and its Cities 发现是一个树上关键点问题 所以考虑虚树刚好也有标志\(\sum k\leq 100000\)即关键点总数的限制. 首先当k==1时 答案显然为0 ...

  3. CF613D Kingdom and its Cities 虚树

    传送门 $\sum k \leq 100000$虚树套路题 设$f_{i,0/1}$表示处理完$i$以及其所在子树的问题,且处理完后$i$所在子树内是否存在$1$个关键点满足它到$i$的路径上不存在任 ...

  4. CF613D Kingdom and its Cities 虚树 + 树形DP

    Code: #include<bits/stdc++.h> #define ll long long #define maxn 300003 #define RG register usi ...

  5. bzoj 3572世界树 虚树+dp

    题目大意: 给一棵树,每次给出一些关键点,对于树上每个点,被离它最近的关键点(距离相同被标号最小的)控制 求每个关键点控制多少个点 分析: 虚树+dp dp过程如下: 第一次dp,递归求出每个点子树中 ...

  6. bzoj 2286 [Sdoi2011]消耗战 虚树+dp

    题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...

  7. 【BZOJ】2286: [Sdoi2011]消耗战 虚树+DP

    [题意]给定n个点的带边权树,每次询问给定ki个特殊点,求隔离点1和特殊点的最小代价.n<=250000,Σki<=500000. [算法]虚树+DP [题解]考虑普通树上的dp,设f[x ...

  8. [BZOJ5287][HNOI2018]毒瘤(虚树DP)

    暴力枚举非树边取值做DP可得75. 注意到每次枚举出一个容斥状态的时候,都要做大量重复操作. 建立虚树,预处理出虚树上两点间的转移系数.也可动态DP解决. 树上倍增.动态DP.虚树DP似乎是这种问题的 ...

  9. [BZOJ2286][SDOI2011]消耗战(虚树DP)

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4998  Solved: 1867[Submit][Statu ...

  10. BZOJ 3572 [HNOI2014]世界树 (虚树+DP)

    题面:BZOJ传送门 洛谷传送门 题目大意:略 细节贼多的虚树$DP$ 先考虑只有一次询问的情况 一个节点$x$可能被它子树内的一个到x距离最小的特殊点管辖,还可能被管辖fa[x]的特殊点管辖 跑两次 ...

随机推荐

  1. 【转载】教你使用 Reflexil 反编译.NET

    简介 反编译的方式有很多种,其实最靠谱的还是IL反编译. 如果不懂IL可以尝试我这边文章入门:http://www.wxzzz.com/278.html 不过我下面要说的不是IL这种底层的代码反编译, ...

  2. (webstorm的CSS插件)前端开发必备!Emmet使用手册

    介绍 Emmet (前身为 Zen Coding) 是一个能大幅度提高前端开发效率的一个工具: 基本上,大多数的文本编辑器都会允许你存储和重用一些代码块,我们称之为“片段”.虽然片段能很好地推动你得生 ...

  3. ArrayList和Vector的区别?HashMap和HashTable的区别?StringBuilder、StringBuffer和String的区别?

    ArrayList和Vector的区别?从两个方面 1.同步性:ArrayList是线程不安全的,是非同步的:Vector是线程安全的,是同步的.(Java中线程的同步也就满足了安全性) 2.数值增长 ...

  4. excel表格定义导入到powerdesigner脚本

    打开powerdesigner,shift + ctrl + X 打开脚本窗口 输入执行的脚本,点 run 即可. 简单的导入Excel脚本 '开始 Option Explicit Dim mdl ' ...

  5. ASP.NET动态网站制作(8)-- JS(3)

    前言:JS的第三节课,这节课主要讲函数.对象及方法. 内容: 1.九九乘法表例子: HTML代码: <!DOCTYPE html> <html xmlns="http:// ...

  6. urllib库python2和python3具体区别

      Python 2 name Python 3 name urllib.urlretrieve() urllib.request.urlretrieve() urllib.urlcleanup() ...

  7. MANIFEST.MF 文件内容完全详解(转)

    打开Java的JAR文件我们经常可以看到文件中包含着一个META-INF目录, 这个目录下会有一些文件,其中必有一个MANIFEST.MF,这个文件描述了该Jar文件的很多信息,下面将详细介绍MANI ...

  8. intellij idea同一个窗口打开多个项目

    1 将多个maven项目放在同一个目录 2 file --> open该目录 intellij 将会加载该目录下所有的项目. 3 依赖加载 maven projects--->" ...

  9. 箭头函数 this arrow function 无this

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions import w ...

  10. consistence availability partition tolerance quit

    理论证明