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. <进阶版>Markdown指南

    有道云笔记内置Markdown编辑器和使用指南. “进阶版”有道云笔记Markdown指南,教你如何进一步掌握待办.清单.流程图和甘特图. 0 待办和清单 待办事项和清单在日常工作.生活中经常被使用. ...

  2. Android ObjectOutputStream Serializable引发的血案

    遇到一个问题 安装后第二次进app,闪退 重现步骤 [前置条件] 打包分支:dev_7.13 手机:vivo NEX 8.1.0 [步骤] 安装三星app----同意用户协议进入书城---连续点击ba ...

  3. uiautomatorviewer定位App元素

    这个工具是Android SDK自带的, 日常的工作中经常要使用的, 在C:\Android\sdk\tools\bin目录下: 双击之, 请注意, 我一般选择第一个机器人小图标Device Scre ...

  4. 破解IDEA注册码,设置 license server一直有效不过期

    破解的详细过程: 1.从下面地址下载一个jar包,名称是  JetbrainsCrack-2.10-release-enc.jar 下载地址是http://idea.lanyus.com/,进去之后点 ...

  5. Java enum类型笔记

    用途: 定义命令行参数,菜单选项,星期,方向(东西南北)等 与普通类的不同 有默认的方法 value() 每个enum类都已默认继承java.lang.Enum,所以enum类不能继承其他类 构造方法 ...

  6. 只写Python一遍代码,就可以同时生成安卓及IOS的APP,真优秀

    前言: 用Python写安卓APP肯定不是最好的选择,但是肯定是一个很偷懒的选择 我们使用kivy开发安卓APP,Kivy是一套专门用于跨平台快速应用开发的开源框架,使用Python和Cython编写 ...

  7. 孤荷凌寒自学python第八十三天初次接触ocr配置tesseract环境

    孤荷凌寒自学python第八十三天初次接触ocr配置tesseract环境 (完整学习过程屏幕记录视频地址在文末) 学习Python我肯定不会错过图片文字的识别,当然更重要的是简单的验证码识别了,今天 ...

  8. Elasticsearch 排序插件的开发

    直接观察到的几个问题 简单expression脚本的执行效率 > java 插件,10000条数据可以测试出1ms左右的差距. Es会不断调用newScript来创建"足够多" ...

  9. Kali渗透测试-SNMP

    1.snmpwalk -v指定snmpwalk版本 -c指定密码 2.snmp-check 获取系统信息,主机名,操作系统及架构 获取用户账户信息 获取网络信息 获取网络接口信息 IP信息 路由信息 ...

  10. 安装HIVE

    参考:https://cwiki.apache.org/confluence/display/Hive/GettingStarted 1.下载hive安装包     到apache官网或者其它地方下载 ...