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.
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.
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.
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
7
1 2
2 3
3 4
1 5
5 6
5 7
1
4 2 4 6 7
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.
正解:虚树+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 ;
}