# 【CF613D】 Kingdom and its Cities

## 题目

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 nn cities. Cities are connected by n-1n−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.

The first line of the input contains integer nn ( 1<=n<=1000001<=n<=100000 ) — the number of cities in the kingdom.

Each of the next n-1n−1 lines contains two distinct integers u_{i}u
i

, v_{i}v
i

( 1<=u_{i},v_{i}<=n1<=u
i

,v
i

<=n ) — the indices of the cities connected by the ii -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 qq ( 1<=q<=1000001<=q<=100000 ) — the number of King’s plans.

Each of the next qq lines looks as follows: first goes number k_{i}k
i

— the number of important cities in the King’s plan, ( 1<=k_{i}<=n1<=k
i

<=n ), then follow exactly k_{i}k
i

space-separated pairwise distinct numbers from 1 to nn — the numbers of important cities in this plan.

The sum of all k_{i}k
i

's does’t exceed 100000100000 .

For each plan print a single integer — the minimum number of cities that the barbarians need to capture, or print -1−1 if all the barbarians’ attempts to isolate important cities will not be effective.

4
1 3
2 3
4 3
4
2 1 2
3 2 3 4
3 1 2 4
4 1 2 3 4

1
-1
1
-1

7
1 2
2 3
3 4
1 5
5 6
5 7
1
4 2 4 6 7

2

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.

## 思路

1.如果x是关键节点，那么如果他的儿子也是关键节点，temp+=dp（儿子）+1即可，如果他的儿子是非关键节点（也就是一个lca点），那么我们把要考虑的情况扔给这个lca点，在当前层直接temp+=dp（儿子）。

2.如果x是非关键节点，也就是x是某个lca，我们考虑他的各个子树，如果有大于一个子树能传递上来关键点，很显然x点必须封堵，那么return temp+1。如果只有一个子树能传递上来关键点，那么lca号点不必封堵，直接将这个关键点信息继续向上传递。如果没有子树能传递上来关键点，就什么也不用管。

if(g[e[x][i]]||bj[e[x][i]]) t++; //t表示有几个子树能传递关键点

1并且在虚树中x的父亲是关键节点时，直接返回temp+1（这就是前文提到的“把要考虑的情况扔给这个lca点”）

## 代码

#include<bits/stdc++.h>
using namespace std;
#define N 200010
int a[N],stop,stac[N],vist,dfn[N],h[N],cnt,top[N],siz[N],son[N],dep[N],fa[N],st[N],n,m,q,ans,ance;
struct node { int y,next; }edg[N<<1];
bool cmp(int x,int y) { return dfn[x]<dfn[y]; }
void add(int x,int y) { edg[++cnt].next=h[x],edg[cnt].y=y,h[x]=cnt; }
void dfs1(int x,int fat){
siz[x]=1;
for(int i=h[x];i;i=edg[i].next) {
int y=edg[i].y;if(y==fat) continue;
dep[y]=dep[fa[y]=x]+1,dfs1(y,x),siz[x]+=siz[y];
if(siz[son[x]]<siz[y]) son[x]=y;
}
}
void dfs2(int x) {
dfn[x]=++vist;
if(!son[x]) return;
top[son[x]]=top[x],dfs2(son[x]);
for(int i=h[x];i;i=edg[i].next) {
int y=edg[i].y;if(y==fa[x]||y==son[x]) continue;
dfs2(top[y]=y);
}
}
int lca(int x,int y) {
while(top[x]^top[y]) {
if(dep[top[x]]>dep[top[y]]) x=fa[top[x]];
else y=fa[top[y]];
}
if(dep[x]<dep[y]) return x;return y;
}
void ins(int x) {
if(!stop) { stac[stop=1]=x;return; }
int anc=lca(stac[stop],x);
while(stop&&dep[anc]<dep[stac[stop]]) add(stac[stop-1],stac[stop]),--stop;
if(!stop||stac[stop]!=anc) stac[++stop]=anc;
stac[++stop]=x;
}
void dfs3(int x) {
if(siz[x]) {
for(int i=h[x];i;i=edg[i].next) {
int y=edg[i].y;dfs3(y);
if(siz[y]) siz[y]=0,++ans;
}
}
else {
for(int i=h[x];i;i=edg[i].next) {
int y=edg[i].y;
dfs3(y),siz[x]+=siz[y],siz[y]=0;
}
if(siz[x]>1) ++ans,siz[x]=0;
}
h[x]=0;
}
int main() {
n=read();
for(int i=1;i<n;++i) {
int x=read(),y=read();
add(x,y),add(y,x);
}
dfs1(dep[1]=1,0),dfs2(top[1]=1),q=read(),vist=0;;
memset(h,0,sizeof(h)),memset(siz,0,sizeof(siz));
for(int i=1;i<=q;++i) {
int x=1;m=read();
for(int i=1;i<=m;++i) a[i]=read(),siz[a[i]]=1;
for(int i=1;i<=m;++i) if(siz[fa[a[i]]]) { printf("-1\n"),x=0;break; }
if(!x) { while(m) siz[a[m]]=0,--m;continue; }
ans=0,sort(a+1,a+m+1,cmp);
if(a[1]!=1) stac[stop=1]=1;
for(int i=1;i<=m;++i) ins(a[i]);
if(stop) while(--stop) add(stac[stop],stac[stop+1]);
dfs3(1),siz[1]=vist=0,printf("%d\n",ans);
}
}


09-26 17

02-10 19

02-22 879

04-05 8

03-19 36

03-14 767

05-28 145

11-28 117

07-04 33

12-07 12

03-10 124

04-14 56万+

03-13 14万+

02-27 7万+

02-28 4万+

03-01 12万+

03-01 11万+

03-01 1万+

03-03 2万+

05-23 3995

03-04 12万+

03-04 3379

03-05 5万+

03-08 4万+

03-08 6万+

03-08 2万+

03-08 1万+

04-25 6万+

03-10 12万+

03-10 17万+

03-12 10万+

03-13 10万+

03-19 7万+

03-22 3万+

03-23 1万+

03-23 4万+

03-24 3万+

03-25 2万+

05-08 4万+

03-25 8万+

03-26 3万+

03-27 4万+

03-29 20万+

03-29 9万+

03-30 14万+

05-21 2921

05-25 5216

03-23 1万+

04-02 3万+

05-06 2万+

04-05 1万+

04-06 896

04-06 6万+

04-07 4万+

04-09 7万+

04-09 2万+

05-17 5330

04-09 4716

04-11 2万+

04-15 5万+

04-18 4万+

04-20 3万+

04-24 2万+

04-26 3675

04-30 7526

05-16 4万+

05-06 1万+

05-08 3万+

05-11 3万+

05-13 6757

05-13 9762

05-14 3666

05-14 6136

05-16 1948

05-16 9355

05-16 1万+

05-17 2355

05-18 1148

05-18 5763

05-18 1596

#### SpringBoot + Vue + Electron 开发 QQ 版聊天工具

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客