zoukankan      html  css  js  c++  java
  • [CF613D]Kingdom and its Cities

    description

    题面

    data range

    [n, q,sum kle 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
    ",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;
    }
    
    
  • 相关阅读:
    WCF异常管理—不要在using语句中调用WCF服务(z)
    正则表达式
    nginx汇总(z)
    WCF编解码实现
    zTree -- jQuery 树插件
    C# 程序异常管理方案
    WPF剪切板问题-OpenClipboard HRESULT:0x800401D0 (CLIPBRD_E_CANT_OPEN))
    乘法逆元
    二叉树后序遍历(非递归)
    P1892 [BOI2003]团伙
  • 原文地址:https://www.cnblogs.com/cjfdf/p/9495029.html
Copyright © 2011-2022 走看看