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.
简单题意
给你一棵树,树上有n个点,然后给q个询问,每次给一些点,求最少要删掉多少点(不能删掉给出的点)才能使这些点分开(给出的总点数小于等于10^5)
胡说题解
其实我们能想到,跟答案有关的点其实就是这些点之间的LCA,其实就是要我们构造这颗虚树,然后在这个虚树上面DP
然后就是虚树的构造方法,按dfs序排序,然后动态的向虚树中加点,我们用栈来维护这颗虚树的最右边的那条链(因为我们按照dfs序排序了,加点只会跟最右边的这条链有关),每次新的点与栈顶的点求LCA,然后根据高度将栈顶的一些点弹出(自己画画图),再加入LCA和新点,这里要注意边怎么连(还是自己多画画图,分情况讨论)
将虚树构造出来后我们就可以在虚树上DP了,这个DP还比较好想,我就不多说了
傻逼错误
一开始想到求LCA,然后就开始乱搞,排序之后直接求LCA然后看这个点是不是给出的点,然后过了两个样例开开心心的交了,然后就傻逼了,WA on test3
然后各种错误,过了好久才知道要求虚树,临时学虚树怎么求,反正各种坑
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 using namespace std; 5 6 const int maxn=100100; 7 int n,q,first[maxn],last[maxn*2],next[maxn*2],fa[maxn],tot,num,time[maxn],i; 8 int ll[maxn*2][20],rr[maxn*2][20],l[maxn],dep[maxn],s[maxn],stack[maxn]; 9 bool flag[maxn]; 10 11 void insert(int x,int y){ 12 if(x&y==0)return; 13 last[++tot]=y; 14 next[tot]=first[x]; 15 first[x]=tot; 16 } 17 18 bool cmp(int a,int b){ 19 return l[a]<l[b]; 20 } 21 22 bool cmp2(int a,int b){ 23 return dep[a]<dep[b]; 24 } 25 26 void dfs(int x,int d){ 27 flag[x]=true; 28 dep[x]=d; 29 ll[++num][0]=x; 30 l[x]=num; 31 int i=first[x]; 32 while(i!=0){ 33 if(!flag[last[i]]){ 34 dfs(last[i],d+1); 35 ll[++num][0]=x; 36 fa[last[i]]=x; 37 } 38 i=next[i]; 39 } 40 } 41 42 int lca(int a,int b){ 43 int tmp; 44 tmp=l[b]-l[a]+1; 45 tmp=trunc(log(tmp)/log(2)); 46 if(cmp2(ll[l[a]][tmp],rr[l[b]][tmp]))tmp=ll[l[a]][tmp]; 47 else tmp=rr[l[b]][tmp]; 48 return tmp; 49 } 50 51 int dp(int x){ 52 int tmp=0,num=0,j=first[x]; 53 while(j!=0){ 54 tmp+=dp(last[j]); 55 if(time[last[j]]==2*i)++num; 56 j=next[j]; 57 } 58 if(time[x]==2*i)tmp+=num; 59 else 60 if(num>1)++tmp; 61 else if(num==1)time[x]=2*i; 62 return tmp; 63 } 64 65 int main(){ 66 scanf("%d",&n); 67 int x,y; 68 for(i=1;i<n;i++){ 69 scanf("%d%d",&x,&y); 70 insert(x,y); 71 insert(y,x); 72 } 73 dfs(1,0); 74 for(i=1;i<=num;i++)rr[i][0]=ll[i][0]; 75 for(i=1;1<<(i-1)<num;i++){ 76 for(x=1;num-x>=1<<(i-1);x++) 77 if(cmp2(ll[x][i-1],ll[x+(1<<(i-1))][i-1]))ll[x][i]=ll[x][i-1]; 78 else ll[x][i]=ll[x+(1<<(i-1))][i-1]; 79 for(x=num-(1<<(i-1))+1;x<=num;x++)ll[x][i]=ll[x][i-1]; 80 for(x=1;num-x>=1<<(i-1);x++) 81 if(cmp2(rr[x][i-1],rr[x+(1<<(i-1))][i-1]))rr[x+(1<<(i-1))][i]=rr[x][i-1]; 82 else rr[x+(1<<(i-1))][i]=rr[x+(1<<(i-1))][i-1]; 83 for(x=1;x<=1<<(i-1);x++)rr[x][i]=rr[x][i-1]; 84 } 85 scanf("%d",&q); 86 bool f; 87 int tmp,now,lasti; 88 for(i=1;i<=q;i++){ 89 scanf("%d",&x); 90 for(y=1;y<=x;y++)scanf("%d",&s[y]); 91 sort(s+1,s+1+x,cmp); 92 f=true;y=x; 93 stack[1]=s[1];now=1; 94 for(x=1;x<=y;x++)time[s[x]]=2*i; 95 tot=0;first[s[1]]=0; 96 for(x=2;x<=y;x++){ 97 lasti=0;first[s[x]]=0; 98 tmp=lca(stack[now],s[x]); 99 if(time[tmp]==2*i && fa[s[x]]==tmp)f=false; 100 if(!f)break; 101 while(now>0 && dep[stack[now]]>dep[tmp])lasti=stack[now--]; 102 if(time[tmp]<i*2-1)time[tmp]=2*i-1,first[tmp]=0; 103 if(stack[now]==tmp){ 104 insert(tmp,s[x]); 105 stack[++now]=s[x]; 106 } 107 else{ 108 first[stack[now]]=next[first[stack[now]]]; 109 insert(stack[now],tmp); 110 insert(tmp,lasti); 111 insert(tmp,s[x]); 112 stack[++now]=tmp; 113 stack[++now]=s[x]; 114 } 115 } 116 if(!f)printf("-1\n"); 117 else printf("%d\n",dp(stack[1])); 118 } 119 return 0; 120 }