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. 每天看一片代码系列(三):codepen上一个音乐播放器的实现

    今天我们看的是一个使用纯HTML+CSS+JS实现音乐播放器的例子,效果还是很赞的: codePen地址 HTML部分 首先我们要思考一下,一个播放器主要包含哪些元素.首先要有播放的进度信息,还有播放 ...

  2. solr 学习

    点击dataimport 没有handler数据  重启下 tomcat 如果没有权限 Cannot find ./catalina.shThe file is absent or does not ...

  3. L010小结后自考题

    . 查询2号分区的inode和block的数量和尺寸 . 在lcr文件夹下创建一个a文件夹,然后进入文件夹中,创建3个3层目录,5个1层目录,5个文件 . 滤出a文件夹下的所有一级目录(4种方法) . ...

  4. eclipse导入jmeter3.1源码并运行

    jmeter3.1源码地址:https://archive.apache.org/dist/jmeter/source/ 1.打开eclipse,新建一个java project的项目,并点击next ...

  5. Java图片转字符

    很久都没有更新博客了,昨天下午一个朋友问我能不能将一张图片转换成字符画,然后我想我这个朋友不知道,也许有的朋友以不知道,我就简单的分享一下 package com.xsl.zhuanhuan; imp ...

  6. 100万套PPT模板,包含全宇宙所有主题类型PPT,绕宇宙100圈,持续更新

    100万套PPT模板,包含全宇宙所有主题类型PPT(全部免费,都是精品,没有一张垃圾不好看的PPT,任何一张PPT拿来套入自己的信息就可以立马使用),绕宇宙100圈,任意一个模板在某文库上都价不菲.强 ...

  7. 「日常训练&知识学习」树的直径(POJ-1849,Two)

    题意 一个城市由节点和连接节点的街道组成,街道是双向的. 此刻大雪覆盖了这个城市,市长确定了一些街道要将它们清扫干净,这些街道保证所有的节点可以通过它们连通而且街道数目尽可能小. 现有两台相同的扫雪机 ...

  8. Unity热更新文件的服务器部署(IIS)

    1.VS新建一个"ASP.NET空网站" 工程结构如下 最好设置.Net FrameWork版本为 V4.0或者V4.5版本的,因为我们的程序最后是要部署到阿里云的虚拟服务器上的, ...

  9. spring入门(Ioc的理解)

    spring对依赖的注入理解可以参考这篇:https://www.cnblogs.com/alltime/p/6729295.html 依赖注入和控制反转 传统的JavaEE程序中,直接在内部new一 ...

  10. leetcode-位1的个数(位与运算)

    位1的个数 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 00000 ...