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.dom
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.ide
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.this
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.spa
Help the King to calculate this characteristic for each of his plan.rest
The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cities in the kingdom.code
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.orm
The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of King's plans.blog
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.ci
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
而後各類錯誤,過了很久才知道要求虛樹,臨時學虛樹怎麼求,反正各類坑
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 }