description

题面

data range

\[n, q,\sum k\le 10^5
\]

solution

还是虚树的练手题

\(f[0/1][u]\)表示\(u\)的子树内,\(u\)是否和重要城市连通的最小分割代价

分类讨论有点捉急

code

#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<complex>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<ctime>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define FILE "a"
#define mp make_pair
#define pb push_back
#define RG register
#define il inline
using namespace std;
typedef unsigned long long ull;
typedef vector<int>VI;
typedef long long ll;
typedef double dd;
const dd eps=1e-10;
const int mod=998244353;
const int N=2000010;
const dd pi=acos(-1);
const int inf=2147483645;
const ll INF=1e18+1;
const ll P=100000;
il ll read(){
RG ll data=0,w=1;RG char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch<='9'&&ch>='0')data=data*10+ch-48,ch=getchar();
return data*w;
} il void file(){
srand(time(NULL)+rand());
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
} int n,m,q,k;
int head[N],nxt[N<<1],to[N<<1],cnt;
int dhead[N],dnxt[N<<1],dto[N<<1],dcnt;
il void addedge(int u,int v){
dto[++dcnt]=v;
dnxt[dcnt]=dhead[u];
dhead[u]=dcnt;
}
int fa[N],dep[N],sz[N],son[N],top[N],dfn[N],low[N],tot;
void dfs1(int u,int ff){
fa[u]=ff;dep[u]=dep[ff]+1;sz[u]=1;
for(RG int i=head[u];i;i=nxt[i]){
RG int v=to[i];if(v==ff)continue;
dfs1(v,u);sz[u]+=sz[v];
if(sz[son[u]]<sz[v])son[u]=v;
}
}
void dfs2(int u,int tp){
top[u]=tp;dfn[u]=++tot;
if(son[u])dfs2(son[u],tp);
for(RG int i=head[u];i;i=nxt[i]){
RG int v=to[i];if(v==fa[u]||v==son[u])continue;
dfs2(v,v);
}
low[u]=++tot;
}
il int lca(int u,int v){
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
u=fa[top[u]];
}
return dep[u]<dep[v]?u:v;
} int mark[N],s[N],cal[N],tp,flg;
bool cmp_dfn(int i,int j){return dfn[i]<dfn[j];}
int f[2][N];
void solve(int u){
f[0][u]=f[1][u]=inf;
RG int sum=0;
if(mark[u]){
for(RG int i=dhead[u];i;i=dnxt[i]){
RG int v=dto[i];solve(v);
sum+=min(f[0][v],f[1][v]+1);
}
f[1][u]=sum;
}
else{
RG int mx=0,tot=0; for(RG int i=dhead[u];i;i=dnxt[i]){
RG int v=dto[i];solve(v);
sum+=min(f[0][v],f[1][v]);
if(f[0][v]>f[1][v])mx=1;
}
f[0][u]=sum+mx; mx=tot=sum=0;
for(RG int i=dhead[u];i;i=dnxt[i]){
RG int v=dto[i];
sum+=min(f[0][v],f[1][v]+1);
if(dep[u]==dep[v]-1&&mark[v])tot++;
else if(f[0][v]>=f[1][v]+1)mx=1;
}
if(tot==1)f[1][u]=sum-1;
else if(!tot&&mx)f[1][u]=sum-1;
}
} int main()
{
n=read();
for(RG int i=1,u,v;i<n;i++){
u=read();v=read();
to[++cnt]=v;nxt[cnt]=head[u];head[u]=cnt;
to[++cnt]=u;nxt[cnt]=head[v];head[v]=cnt;
}
dfs1(1,0);dfs2(1,1); q=read();
for(RG int i=1;i<=q;i++){
m=read();tp=k=dcnt=flg=0;
for(RG int j=1;j<=m;j++)s[++k]=read(),mark[s[k]]=1;
sort(s+1,s+k+1,cmp_dfn);
for(RG int j=1;j<m;j++)s[++k]=lca(s[j],s[j+1]);s[++k]=1;
sort(s+1,s+k+1,cmp_dfn);k=unique(s+1,s+k+1)-s-1;
for(RG int j=1;j<=k;j++){
while(tp&&low[cal[tp]]<dfn[s[j]])tp--;
if(tp){
if(dep[cal[tp]]==dep[s[j]]-1&&mark[cal[tp]]&&mark[s[j]])
flg=1;
addedge(cal[tp],s[j]);
}
cal[++tp]=s[j];
}
if(!flg){solve(1);printf("%d\n",min(f[0][1],f[1][1]));}
else puts("-1");
for(RG int j=1;j<=k;j++)mark[s[j]]=dhead[s[j]]=0;
}
return 0;
}

[CF613D]Kingdom and its Cities的更多相关文章

  1. CF613D Kingdom and its Cities 虚树 树形dp 贪心

    LINK:Kingdom and its Cities 发现是一个树上关键点问题 所以考虑虚树刚好也有标志\(\sum k\leq 100000\)即关键点总数的限制. 首先当k==1时 答案显然为0 ...

  2. CF613D Kingdom and its Cities 虚树

    传送门 $\sum k \leq 100000$虚树套路题 设$f_{i,0/1}$表示处理完$i$以及其所在子树的问题,且处理完后$i$所在子树内是否存在$1$个关键点满足它到$i$的路径上不存在任 ...

  3. CF613D:Kingdom and its Cities(树形DP,虚树)

    Description 一个王国有n座城市,城市之间由n-1条道路相连,形成一个树结构,国王决定将一些城市设为重要城市. 这个国家有的时候会遭受外敌入侵,重要城市由于加强了防护,一定不会被占领.而非重 ...

  4. CF613D Kingdom and its Cities 虚树 + 树形DP

    Code: #include<bits/stdc++.h> #define ll long long #define maxn 300003 #define RG register usi ...

  5. CF613D Kingdom and its Cities(虚树+贪心)

    很休闲的一个题啊 其实一看到关于\(\sum k\)的限制,就知道是个虚树的题了 首先我们把虚树建出来,然后考虑怎么计算个数呢? 我们令\(f[x]\)表示以\(x\)的子树中,剩余了多少个还没有切断 ...

  6. 【CF613D】Kingdom and its Cities 虚树+树形DP

    [CF613D]Kingdom and its Cities 题意:给你一棵树,每次询问给出k个关键点,问做多干掉多少个非关键点才能使得所有关键点两两不连通. $n,\sum k\le 10^5$ 题 ...

  7. 【CF613D】Kingdom and its Cities

    [CF613D]Kingdom and its Cities 题面 洛谷 题解 看到关键点当然是建虚树啦. 设\(f[x]\)表示以\(x\)为根的子树的答案,\(g[x]\)表示以\(x\)为根的子 ...

  8. 【CF613D】Kingdom and its Cities(虚树,动态规划)

    [CF613D]Kingdom and its Cities(虚树,动态规划) 题面 洛谷 CF 翻译洛谷上有啦 题解 每次构建虚树,首先特判无解,也就是关键点中存在父子关系. 考虑\(dp\),设\ ...

  9. Kingdom and its Cities - CF613D

    Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in or ...

随机推荐

  1. 【LG4175】[CTSC2008]网络管理

    [LG4175][CTSC2008]网络管理 题面 洛谷 题解 感觉就和普通的整体二分差不太多啊... 树上修改就按时间添加,用树状数组维护一下即可 代码 #include<iostream&g ...

  2. Filter配置多个url-pattern

    java开发中会用的Filter过滤器,有时候开发需要,在一个Filter中需要配置多个过滤地址,即<url-pattern>,下面就说一下一个Filter过滤器中多个<url-pa ...

  3. 全局脚手架了解一下【fle-cli】

    本文来自网易云社区 介绍 fle-cli旨在帮助我们从复杂繁琐的编译配置中解放出来,全身心地投入业务开发中,提高开发效率. 它是真正意义上的全局脚手架,区别于市面上其他的全局脚手架,它不会在项目工程中 ...

  4. Appium的环境搭建和配置

    Appium的环境搭建和配置 一.安装Nodejs 下载nodejs安装包(https://nodejs.org/en/download/)安装 下载后,双击安装文件,按提示来安装. 测试安装是否成功 ...

  5. Appium的一点一滴:Android KEYCODE键值

    转自:http://blog.csdn.net/crisschan/article/details/50419963 - 电话键 键名 描述 键值 KEYCODE_CALL 拨号键 5 KEYCODE ...

  6. Linux命令应用大词典-第33章 X Window

    33.1 xhost:X服务器的访问控制程序 33.2 xinit:X Window系统初始化 33.3 Xlsclients:在显示器中列出正在运行的客户端应用程序 33.4 xlsfonts:显示 ...

  7. ServiceStack.Ormlit sqlserver枚举类型映射字段类型为varchar

    请当枚举类型上面加上[Flags]特性就可以了.

  8. OpenCV学习5-----使用Mat合并多张图像

    最近做实验需要对比实验结果,需要将几张图片拼在一起,直观对比. 尝试用OpenCV解决. 核心思想其实是   声明一个足够大的,正好容纳下那几张图片的mat,然后将拼图依次copy到大图片相应的位置. ...

  9. HDU 2491 Priest John's Busiest Day(贪心)(2008 Asia Regional Beijing)

    Description John is the only priest in his town. October 26th is the John's busiest day in a year be ...

  10. HDU 4169 Wealthy Family(树形DP)

    Problem Description While studying the history of royal families, you want to know how wealthy each ...