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. 长沙Uber优步司机奖励政策(1月4日~1月10日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. QtChart 初体验

    早就知道 Qt 5.7 中引入了 QtChart 模块.一直没时间试用.周末正好空闲,就简单的试了试 QtChart.QtChart 学起来还是挺简单的,基于 Qt Graphics View Fra ...

  3. 手写ORM第一版

    ORM第一版: #Author = __rianley cheng__ #ORM 简易版 from mysql_ import Mysql class Fileld: def __init__(sel ...

  4. Redis系列一 Redis安装

    Redis系列一    Redis安装 1.安装所使用的操作系统为Ubuntu16.04 Redis版本为3.2.9 软件一般下载存放目录为/opt,以下命令操作目录均为/opt root@ubunt ...

  5. elasticsearch安装中文分词器

    1. 分词器的安装 ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/rele ...

  6. liunx环境下安装禅道

    环境: vm12.5.2 CentOS-7-x86_64 ZenTaoPMS.9.1.stable.zbox_64 SecureCRT 8.0 因为liunx环境下配置apache, php, mys ...

  7. 用Python实现一个端口扫描,只需简单几步就好

    一.常见端口扫描的原理 0.秘密扫描 秘密扫描是一种不被审计工具所检测的扫描技术. 它通常用于在通过普通的防火墙或路由器的筛选(filtering)时隐藏自己. 秘密扫描能躲避IDS.防火墙.包过滤器 ...

  8. (Pyhton爬虫03)爬虫初识

    原本的想法是这样的:博客整理知识学习的同时,也记录点心情...集中式学习就没这么多好记录的了! 要学习一门技术,首先要简单认识一下爬虫!其实可以参考爬虫第一章! 整体上介绍该技术包含技能,具体能做什么 ...

  9. vector的基础使用

    vector是一个容器,实现动态数组. 相似点:下标从0开始. 不同点:vector创建对象后,容器大小会随着元素的增多或减少而变化. 基础操作: 1.使用vector需要添加头文件,#include ...

  10. java基础-Comparator接口与Collections实现排序算法

    java 排序Comparable和Comparator使用 java提供了两个排序用的接口Comparable和Comparator,一般情况下使用区别如下: Comparable 接口用于类的固定 ...