CodeForces - 613D:Kingdom and its Cities(虚树+DP)
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.
Examples
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
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.
题意:这两天看了不少的关于虚树的题目,有些许收获,然后此题算是最简单,先写此题。
思路:用DP或者贪心可以解决此题,但是由于每一次的复杂度都是O(N),而每一次的可用点是不多的,注意到sigma(K)<1e5,显然需要建立虚树,在虚树上做DP或者贪心。
看到很多种虚树的写法,这个比较好记:
1,先按DFS序排序,然后得到相邻的LCA,加入点集,去重。
2,建虚树,维护一个栈,对于当前要加入的点,如果它不在栈顶的点的子树内(也就是代码里的belong,用dfs序很方便的判断),我们就一直退栈,然后栈顶向当前点连边即可。(队列里维护的是一条从根到此点的dep递增的DFS链)
得到虚树后就可以DP了:
从下向上遍历,如果当前点是染色的点,那么它需要和染色儿子切断。如果不是染色的点,当染色儿子大于1时,需要去掉此点;当染色儿子等于1时,不用管。
#include<bits/stdc++.h>
const int maxn=;
using namespace std;
int Laxt[maxn],Next[maxn<<],To[maxn<<],dep[maxn],cnt;
int in[maxn],out[maxn],fa[maxn][],times,a[maxn];
int q[maxn],col[maxn],sz[maxn],top,tot,ans;
vector<int>G[maxn];
void add(int u,int v){ Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; }
bool cmp(int x,int y){ return in[x]<in[y]; }
void dfs1(int u,int f)
{
in[u]=++times; fa[u][]=f; dep[u]=dep[f]+;
for(int i=Laxt[u];i;i=Next[i]) if(To[i]!=f) dfs1(To[i],u);
out[u]=times;
}
int LCA(int u,int v)
{
if(dep[u]<dep[v]) swap(u,v);
for(int i=;i>=;i--) if(dep[fa[u][i]]>=dep[v]) u=fa[u][i];
if(u==v) return u;
for(int i=;i>=;i--) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
return fa[u][];
}
bool belong(int x,int y){ return in[x]>=in[y]&&in[x]<=out[y]; } //判断x是否在y的子树里。
bool check()
{
for(int i=;i<=tot;i++)
if(col[fa[a[i]][]]) return false;
return true;
}
void build() //建立虚树
{
top=; for(int i=;i<=tot;i++){
while(top&&!belong(a[i],q[top])) top--;
if(top) G[q[top]].push_back(a[i]);
q[++top]=a[i];
}
}
void dfs2(int u) //DP
{
int L=G[u].size();
for(int i=;i<L;i++){
dfs2(G[u][i]);
sz[u]+=sz[G[u][i]];
}
if(col[u]) ans+=sz[u],sz[u]=;
else if(sz[u]>) ans++,sz[u]=;
}
int main()
{
int N,Q,K,u,v,i,j;
scanf("%d",&N);
for(i=;i<N;i++){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
dfs1(,);
for(i=;i<=;i++)
for(j=;j<=N;j++)
fa[j][i]=fa[fa[j][i-]][i-];
scanf("%d",&Q);
while(Q--){
scanf("%d",&K); tot=K; ans=;
for(i=;i<=K;i++) scanf("%d",&a[i]),col[a[i]]=;
if(!check()){ puts("-1"); for(i=;i<=K;i++) col[a[i]]=; continue; }
sort(a+,a+tot+,cmp);
for(i=;i<=K;i++) a[++tot]=LCA(a[i-],a[i]);
sort(a+,a+tot+,cmp);
tot=unique(a+,a+tot+)-(a+);
build();
dfs2(a[]);
printf("%d\n",ans);
for(i=;i<=tot;i++) col[a[i]]=sz[a[i]]=,G[a[i]].clear();
}
return ;
}
CodeForces - 613D:Kingdom and its Cities(虚树+DP)的更多相关文章
- 【CF613D】Kingdom and its Cities 虚树+树形DP
[CF613D]Kingdom and its Cities 题意:给你一棵树,每次询问给出k个关键点,问做多干掉多少个非关键点才能使得所有关键点两两不连通. $n,\sum k\le 10^5$ 题 ...
- CF613D Kingdom and its Cities 虚树 树形dp 贪心
LINK:Kingdom and its Cities 发现是一个树上关键点问题 所以考虑虚树刚好也有标志\(\sum k\leq 100000\)即关键点总数的限制. 首先当k==1时 答案显然为0 ...
- CF613D Kingdom and its Cities 虚树
传送门 $\sum k \leq 100000$虚树套路题 设$f_{i,0/1}$表示处理完$i$以及其所在子树的问题,且处理完后$i$所在子树内是否存在$1$个关键点满足它到$i$的路径上不存在任 ...
- CF613D Kingdom and its Cities 虚树 + 树形DP
Code: #include<bits/stdc++.h> #define ll long long #define maxn 300003 #define RG register usi ...
- bzoj 3572世界树 虚树+dp
题目大意: 给一棵树,每次给出一些关键点,对于树上每个点,被离它最近的关键点(距离相同被标号最小的)控制 求每个关键点控制多少个点 分析: 虚树+dp dp过程如下: 第一次dp,递归求出每个点子树中 ...
- bzoj 2286 [Sdoi2011]消耗战 虚树+dp
题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...
- 【BZOJ】2286: [Sdoi2011]消耗战 虚树+DP
[题意]给定n个点的带边权树,每次询问给定ki个特殊点,求隔离点1和特殊点的最小代价.n<=250000,Σki<=500000. [算法]虚树+DP [题解]考虑普通树上的dp,设f[x ...
- [BZOJ5287][HNOI2018]毒瘤(虚树DP)
暴力枚举非树边取值做DP可得75. 注意到每次枚举出一个容斥状态的时候,都要做大量重复操作. 建立虚树,预处理出虚树上两点间的转移系数.也可动态DP解决. 树上倍增.动态DP.虚树DP似乎是这种问题的 ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- BZOJ 3572 [HNOI2014]世界树 (虚树+DP)
题面:BZOJ传送门 洛谷传送门 题目大意:略 细节贼多的虚树$DP$ 先考虑只有一次询问的情况 一个节点$x$可能被它子树内的一个到x距离最小的特殊点管辖,还可能被管辖fa[x]的特殊点管辖 跑两次 ...
随机推荐
- leetcode_Multiply Strings
描写叙述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- Linux下MySQL定时按日期备份数据
一.使用mysql内置命令 mysqldump Usage: mysqldump [OPTIONS] database [tables] mysqldump [OPTIONS] --databases ...
- hdu 2814 Interesting Fibonacci
pid=2814">点击此处就可以传送 hdu 2814 题目大意:就是给你两个函数,一个是F(n) = F(n-1) + F(n-2), F(0) = 0, F(1) = 1; 还有 ...
- java拷贝构造函数
浅拷贝就是指两个对象共同拥有同一个值,一个对象改变了该值,也会影响到另一个对象. 深拷贝就是两个对象的值相等,但是互相独立. 构造函数的参数是该类的一个实例. Operator = 拷贝构造函数 ...
- 使用mysqld_multi 实现Mysql 5.6.36 + 5.7.18 单机多实例多版本安装
Mysql 5.6.36 + 5.7.18 单机多实例多版本安装 随着硬件层面的发展,各种高性能服务器如雨后春笋般出现,但高性能服务器不免造成浪费, MySQL单机多实例,是指在一台物理服务器上运行多 ...
- Appium python自动化测试系列之使用HTMLTestRunner生成测试报告(十三)
13.1 测试报告概述 13.1.1 测试报告的定义 在前面章节我们已经讲了自动化基础的很多东西,如果说掌握了,而且自己动手去练习了,我相信在一些初级的面试中是没任何问题的,今天我们接触的应该算是一 ...
- 22 nginx配置与集群
一:编译nginx ,并配置 Cd /app/pcre-8.12 ./configure Make && make install Cd nginx-1.2.7 ./configure ...
- Mysql 索引增加与删除
[1]索引 索引,通俗理解,即目录. 之前说过,计算机是对现实世界的模拟.目录应用在数据库领域,即所谓的索引. 目录的作用显而易见,所以建立索引可以大大提高检索的速度. 但是,会降低更新表的速度,如对 ...
- 复习Java虚拟机:JVM中的Stack和Heap
在JVM中,内存分为两个部分,Stack(栈)和Heap(堆).这里,我们从JVM的内存管理原理的角度来认识Stack和Heap,并通过这些原理认清Java中静态方法和静态属性的问题. 一般,JVM的 ...
- Java 学习 day06
01-面向对象(Static关键字) package myFirstCode; /* 静态:static. 用法:是一个修饰符,用于修饰成员(成员变量,成员函数) 当成员被静态修饰后,就多了一个调用方 ...