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. CF 600 E. Lomsat gelral

    E. Lomsat gelral http://codeforces.com/contest/600/problem/E 题意: 求每个子树内出现次数最多的颜色(如果最多的颜色出现次数相同,将颜色编号 ...

  2. ShimmerTextView

    本文来自网易云社区 作者:孙有军 产品中有一个需求,要求TextView的文字有一个高亮的效果,高亮的同时有跑马灯效果! 本来想在网上找一个现成的用用,比如Facebook出的Shimmer,还有很多 ...

  3. tomcat createSecureRandom 花费了将近10分钟

    http://www.th7.cn/Program/java/201603/776312.shtml 启动tomcat很慢,检查后发现:[localhost-startStop-1] org.apac ...

  4. redhat防火墙管理

    systemctl status firewalldsystemctl stop firewalldsystemctl start firewalldsystemctl enable firewall ...

  5. spring使用set方法注入的常见类型写法

    首先配置spring的pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...

  6. Ubuntu16.04比较好的一系列软件安装介绍

    https://blog.csdn.net/Gerald_Jones/article/details/80784976

  7. JEMTER简单的测试计划

    测试计划一 1)测试网站:http://www.geneedu.cn/和http://supu01.1688.com/ 2)测试目的是该网站在负载达到20 QPS 时的响应时间. 备注: QPS : ...

  8. CSS选择器语法&示例

    CSS3 选择器 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. "CSS" 列指示该属性是在哪个 CSS 版本中定义的.(CSS1.CSS2 还是 CSS3.) ...

  9. 软件测试工程师必备的SQL语句基础

    为一个软件测试工程师,我们在测试过程中往往需要对数据库数据进行操作,但是我们的操作大多以查询居多,有时会涉及到新增,修改,删除等操作,所以我们其实并不需要对数据库的操作有特别深入的了解,以下是我在工作 ...

  10. Linux命令应用大词典-第10章 Shell相关命令

    10.1 commond:抑制正常的Shell函数查找 10.2 exec:使用执行命令替换当前的shell进程 10.3 bash:GNU的Bourne-Again Shell解释器 10.4 bu ...