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

简单题意

给你一棵树,树上有n个点,然后给q个询问,每次给一些点,求最少要删掉多少点(不能删掉给出的点)才能使这些点分开(给出的总点数小于等于10^5)

胡说题解

其实我们能想到,跟答案有关的点其实就是这些点之间的LCA,其实就是要我们构造这颗虚树,然后在这个虚树上面DP

然后就是虚树的构造方法,按dfs序排序,然后动态的向虚树中加点,我们用栈来维护这颗虚树的最右边的那条链(因为我们按照dfs序排序了,加点只会跟最右边的这条链有关),每次新的点与栈顶的点求LCA,然后根据高度将栈顶的一些点弹出(自己画画图),再加入LCA和新点,这里要注意边怎么连(还是自己多画画图,分情况讨论)

将虚树构造出来后我们就可以在虚树上DP了,这个DP还比较好想,我就不多说了

傻逼错误

一开始想到求LCA,然后就开始乱搞,排序之后直接求LCA然后看这个点是不是给出的点,然后过了两个样例开开心心的交了,然后就傻逼了,WA on test3

然后各种错误,过了好久才知道要求虚树,临时学虚树怎么求,反正各种坑

 #include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std; const int maxn=;
int n,q,first[maxn],last[maxn*],next[maxn*],fa[maxn],tot,num,time[maxn],i;
int ll[maxn*][],rr[maxn*][],l[maxn],dep[maxn],s[maxn],stack[maxn];
bool flag[maxn]; void insert(int x,int y){
if(x&y==)return;
last[++tot]=y;
next[tot]=first[x];
first[x]=tot;
} bool cmp(int a,int b){
return l[a]<l[b];
} bool cmp2(int a,int b){
return dep[a]<dep[b];
} void dfs(int x,int d){
flag[x]=true;
dep[x]=d;
ll[++num][]=x;
l[x]=num;
int i=first[x];
while(i!=){
if(!flag[last[i]]){
dfs(last[i],d+);
ll[++num][]=x;
fa[last[i]]=x;
}
i=next[i];
}
} int lca(int a,int b){
int tmp;
tmp=l[b]-l[a]+;
tmp=trunc(log(tmp)/log());
if(cmp2(ll[l[a]][tmp],rr[l[b]][tmp]))tmp=ll[l[a]][tmp];
else tmp=rr[l[b]][tmp];
return tmp;
} int dp(int x){
int tmp=,num=,j=first[x];
while(j!=){
tmp+=dp(last[j]);
if(time[last[j]]==*i)++num;
j=next[j];
}
if(time[x]==*i)tmp+=num;
else
if(num>)++tmp;
else if(num==)time[x]=*i;
return tmp;
} int main(){
scanf("%d",&n);
int x,y;
for(i=;i<n;i++){
scanf("%d%d",&x,&y);
insert(x,y);
insert(y,x);
}
dfs(,);
for(i=;i<=num;i++)rr[i][]=ll[i][];
for(i=;<<(i-)<num;i++){
for(x=;num-x>=<<(i-);x++)
if(cmp2(ll[x][i-],ll[x+(<<(i-))][i-]))ll[x][i]=ll[x][i-];
else ll[x][i]=ll[x+(<<(i-))][i-];
for(x=num-(<<(i-))+;x<=num;x++)ll[x][i]=ll[x][i-];
for(x=;num-x>=<<(i-);x++)
if(cmp2(rr[x][i-],rr[x+(<<(i-))][i-]))rr[x+(<<(i-))][i]=rr[x][i-];
else rr[x+(<<(i-))][i]=rr[x+(<<(i-))][i-];
for(x=;x<=<<(i-);x++)rr[x][i]=rr[x][i-];
}
scanf("%d",&q);
bool f;
int tmp,now,lasti;
for(i=;i<=q;i++){
scanf("%d",&x);
for(y=;y<=x;y++)scanf("%d",&s[y]);
sort(s+,s++x,cmp);
f=true;y=x;
stack[]=s[];now=;
for(x=;x<=y;x++)time[s[x]]=*i;
tot=;first[s[]]=;
for(x=;x<=y;x++){
lasti=;first[s[x]]=;
tmp=lca(stack[now],s[x]);
if(time[tmp]==*i && fa[s[x]]==tmp)f=false;
if(!f)break;
while(now> && dep[stack[now]]>dep[tmp])lasti=stack[now--];
if(time[tmp]<i*-)time[tmp]=*i-,first[tmp]=;
if(stack[now]==tmp){
insert(tmp,s[x]);
stack[++now]=s[x];
}
else{
first[stack[now]]=next[first[stack[now]]];
insert(stack[now],tmp);
insert(tmp,lasti);
insert(tmp,s[x]);
stack[++now]=tmp;
stack[++now]=s[x];
}
}
if(!f)printf("-1\n");
else printf("%d\n",dp(stack[]));
}
return ;
}

AC代码

Kingdom and its Cities - CF613D的更多相关文章

  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

    [CF613D]Kingdom and its Cities 题面 洛谷 题解 看到关键点当然是建虚树啦. 设\(f[x]\)表示以\(x\)为根的子树的答案,\(g[x]\)表示以\(x\)为根的子 ...

  3. 【CF613D】Kingdom and its Cities(虚树,动态规划)

    [CF613D]Kingdom and its Cities(虚树,动态规划) 题面 洛谷 CF 翻译洛谷上有啦 题解 每次构建虚树,首先特判无解,也就是关键点中存在父子关系. 考虑\(dp\),设\ ...

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

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

  5. CF613D Kingdom and its Cities 虚树

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

  6. CF613D:Kingdom and its Cities(树形DP,虚树)

    Description 一个王国有n座城市,城市之间由n-1条道路相连,形成一个树结构,国王决定将一些城市设为重要城市. 这个国家有的时候会遭受外敌入侵,重要城市由于加强了防护,一定不会被占领.而非重 ...

  7. [CF613D]Kingdom and its Cities

    description 题面 data range \[n, q,\sum k\le 10^5\] solution 还是虚树的练手题 \(f[0/1][u]\)表示\(u\)的子树内,\(u\)是否 ...

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

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

  9. 题解 CF613D 【Kingdom and its Cities】

    考虑树形\(DP\),设\(num_x\)记录的为当\(1\)为根时,以\(x\)为子树中重要城市的个数. 那么进行分类讨论: ① 当\(num_x≠0\)时,则需将其所有满足\(num_y≠0\)的 ...

随机推荐

  1. LeetCode:49. Group Anagrams(Medium)

    1. 原题链接 https://leetcode.com/problems/group-anagrams/description/ 2. 题目要求 给定一个字符串数组,将数组中包含相同字母的元素放在同 ...

  2. kafka配置参数详解

    Broker  Configs Property Default Description broker.id   每个broker都可以用一个唯一的非负整数id进行标识:这个id可以作为broker的 ...

  3. spring源码-国际化-3.5

    一.国际化在实际代码中是非常常见的一中方式.为了结合web做一下语言上面的切换,而达到展示的目的. 二.这里呢,主要是介绍spring中对于国际化做了哪些处理. 三.实现方式 1)xml配置 < ...

  4. ping telnet 指令

    Ping 一 作用 ping能够辨别网络功能的某些状态,这些状态是日常网络故障诊断的基础.Ping能够识别连接的二进制状态(看是否连通).Ping命令通过过向计算机发送ICMP回应报文并监听回应报文的 ...

  5. 腾讯WeTest受邀参展2018谷歌开发者大会,Android 9专区免费开放

    2018谷歌开发者大会(Google Developer Days)于9月20日正式在上海拉开帷幕.在今年,围绕谷歌最新研发技术,来自机器学习.物联网.云服务等各领域精英参会并进行了案例分享. 201 ...

  6. Use GitHub Desktop to get GitHub projects

    Find the project's https git file in the home page of the project. e.g. https://github.com/PrismLibr ...

  7. 「日常训练」Kefa and Company(Codeforces Round #321 Div. 2 B)

    题意与分析(CodeForces 580B) \(n\)个人,告诉你\(n\)个人的工资,每个人还有一个权值.现在从这n个人中选出m个人,使得他们的权值之和最大,但是对于选中的人而言,其他被选中的人的 ...

  8. javac 编译过程

    javac 编译过程     一.解析与填充符号表: 1.  语法.此法分析:          a) 语法分析:将源代码字符流转换为标记(Token:编译过程最小元素)集合.          b) ...

  9. Bootstrap框架(组件)

    按钮组 通过按钮组容器把一组按钮放在同一行里.通过与按钮插件联合使用,可以设置为单选框或多选框的样式和行为. 按钮组中的工具提示和弹出框需要特别的设置 当为 .btn-group 中的元素应用工具提示 ...

  10. POJ 1921 Paper Cut(计算几何の折纸问题)

    Description Still remember those games we played in our childhood? Folding and cutting paper must be ...