题目连接:点击查看
题目大意:给出一个 p 个点 c 条边的无向图,设置点 1 为基地,现在有 n 个点表示自己没有被摧毁,但无法与基地相连,现在问最少摧毁多少个点可以使得满足条件
题目分析:最小割,割的是点,所以考虑拆点限流:
- 源点 -> 点 1,流量为 inf
- 不能被摧毁的点:入点 -> 出点,流量为 inf
- 可以被摧毁的点:入点 -> 出点,流量为 1
- n 个点的出点 -> 汇点,流量为 inf
跑最大流最小割就是答案了
需要注意的就是,p 是图中节点的个数吧,写成了 n 调了有一会。。
代码:
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e6+100;struct Edge
{int to,w,next;
}edge[N];//边数int head[N],cnt,p,c,n,id1[3100],id2[3100],vis[3100];char s[N];void addedge(int u,int v,int w)
{edge[cnt].to=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].to=u;edge[cnt].w=0;//反向边边权设置为0edge[cnt].next=head[v];head[v]=cnt++;
}int d[N],now[N];//深度 当前弧优化bool bfs(int s,int t)//寻找增广路
{memset(d,0,sizeof(d));queue<int>q;q.push(s);now[s]=head[s];d[s]=1;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(d[v])continue;if(!w)continue;d[v]=d[u]+1;now[v]=head[v];q.push(v);if(v==t)return true;}}return false;
}int dinic(int x,int t,int flow)//更新答案
{if(x==t)return flow;int rest=flow,i;for(i=now[x];i!=-1&&rest;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(w&&d[v]==d[x]+1){int k=dinic(v,t,min(rest,w));if(!k)d[v]=0;edge[i].w-=k;edge[i^1].w+=k;rest-=k;}}now[x]=i;return flow-rest;
}void init()
{memset(now,0,sizeof(now));memset(head,-1,sizeof(head));cnt=0;
}int solve(int st,int ed)
{int ans=0,flow;while(bfs(st,ed))while(flow=dinic(st,ed,inf))ans+=flow;return ans;
}void get_id()
{int id=0;for(int i=1;i<=p;i++){id1[i]=++id;id2[i]=++id;}
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);init();int st=N-1,ed=st-1;scanf("%d%d%d",&p,&c,&n);get_id();while(c--){int x,y;scanf("%d%d",&x,&y);addedge(id2[x],id1[y],inf);addedge(id2[y],id1[x],inf);}while(n--){int x;scanf("%d",&x);vis[x]=true;//不能割的点addedge(id2[x],ed,inf); }vis[1]=true;for(int i=1;i<=p;i++){if(vis[i])addedge(id1[i],id2[i],inf);elseaddedge(id1[i],id2[i],1);}addedge(st,id1[1],inf);printf("%d\n",solve(st,ed));return 0;
}