首页 技术 正文
技术 2022年11月15日
0 收藏 771 点赞 3,270 浏览 2957 个字

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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902