Kingdom and its Cities - CF613D

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 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.

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.

Sample test(s)

Input

41 32 34 342 1 23 2 3 43 1 2 44 1 2 3 4

Output

1-11-1

Input

71 22 33 41 55 65 714 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.

简单题意

给你一棵树,树上有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 }

AC代码

时间: 2024-07-30 00:23:26

Kingdom and its Cities - CF613D的相关文章

CF613D Kingdom and its Cities 虚树

传送门 $\sum k \leq 100000$虚树套路题 设$f_{i,0/1}$表示处理完$i$以及其所在子树的问题,且处理完后$i$点存在$0/1$个没有被封住的关键点时的最小代价,转移考虑$i$是否是关键点,随便转就行了 1 #include<bits/stdc++.h> 2 //This code is written by Itst 3 using namespace std; 4 5 inline int read(){ 6 int a = 0; 7 bool f = 0; 8

CF613D Kingdom and its Cities

题目链接 问题分析 首先看数据范围不难发现是虚树. 但是这个DP怎么写的我这么难受-- 应该是不难的DP,\(F[i][0]\)表示\(i\)不占领,\(F[i][1]\)表示\(i\)占领,然后分类讨论--具体的见代码吧-- 参考程序 #include <bits/stdc++.h> using namespace std; const int Maxn = 100010; const int INF = 1000010; const int MaxLog = 20; struct edge

题解 CF613D 【Kingdom and its Cities】

考虑树形\(DP\),设\(num_x\)记录的为当\(1\)为根时,以\(x\)为子树中重要城市的个数. 那么进行分类讨论: ① 当\(num_x≠0\)时,则需将其所有满足\(num_y≠0\)的儿子\(y\)删去. ② 当\(num_x=0\)时,若满足\(num_y≠0\)的儿子\(y\)个数\(cnt=1\),则直接让\(num\)进行向上传递,若满足\(num_y≠0\)的儿子\(y\)个数\(cnt>1\),则需删去\(x\)本身. 不合法的情况特判掉. 考虑到多次询问和树上点集的

【CF613D】Kingdom and its Cities

题目 题目链接:https://codeforces.com/problemset/problem/613/D 一个王国有 \(n\) 座城市,城市之间由 \(n-1\) 条道路相连,形成一个树结构,国王决定将一些城市设为重要城市. 这个国家有的时候会遭受外敌入侵,重要城市由于加强了防护,一定不会被占领.而非重要城市一旦被占领,这座城市就不能通行. 国王定了若干选择重要城市的计划,他想知道,对于每个计划,外敌至少要占领多少个非重要城市,才会导致重要城市之间两两不连通.如果外敌无论如何都不可能导致

codeforces 613D:Kingdom and its Cities

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 daug

CodeForces - 613D:Kingdom and its Cities(虚树+DP)

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 marri

【模板】虚树

核心思想: (听名字高大上,实际上没什么东西……虚树的题主要难在如何操作虚树) 给出$k$个关键点,我们要建出一棵只包含这些关键点和他们$lca$的点数最少的树,以实现$dp$等操作. 标志性的数据范围是$\sum{k}\leq 10^{5}$之类的. 建树方法: 1.将所有关键点按$dfs$序排序. 2.开一个栈表示根到当前点的虚树路径,并把根丢进去. 3.对于每一个关键点$u$: 若栈中只有根这一个元素,则把$u$丢进去. 否则,我们需要弹出栈中所有不在根到$u$路径上的点. 我们用$lca

Travel(HDU 5441 2015长春区域赛 带权并查集)

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2404    Accepted Submission(s): 842 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is tr

hdu5441

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1225    Accepted Submission(s): 443 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is tr