标签:false swa show space struct pop can max online
题目链接:戳我
算是最大流的模板了吧qwq
建立一个源点和汇点,分别向二分图的两边连边(容量为1)
最后输出方案的时候判断一下边不为0即可。
代码如下
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define MAXN 100010
using namespace std;
int n,m,t=-1;
int head[MAXN],dis[MAXN],cur[MAXN];
struct Edge{int nxt,to,dis;}edge[MAXN<<1];
inline void add(int from,int to,int dis)
{
edge[++t].nxt=head[from],edge[t].to=to,edge[t].dis=dis,head[from]=t;
edge[++t].nxt=head[to],edge[t].to=from,edge[t].dis=0,head[to]=t;
}
inline bool bfs()
{
memset(dis,0x3f,sizeof(dis));
memcpy(cur,head,sizeof(head));
queue<int>q;
q.push(0);
dis[0]=0;
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=head[u];i;i=edge[i].nxt)
{
int v=edge[i].to;
if(dis[v]==0x3f3f3f3f&&edge[i].dis)
{
dis[v]=dis[u]+1;
q.push(v);
}
}
}
if(dis[n+1]==0x3f3f3f3f) return false;
return true;
}
inline int dfs(int x,int f)
{
if(x==n+1||!f) return f;
int used=0,w;
for(int i=cur[x];i;i=edge[i].nxt)
{
cur[x]=i;
if(dis[edge[i].to]==dis[x]+1&&(w=dfs(edge[i].to,min(f,edge[i].dis))))
{
used+=w,f-=w;
edge[i].dis-=w,edge[i^1].dis+=w;
if(!f) break;
}
}
//if(!used) dis[x]=0;
return used;
}
inline int dinic()
{
int cur_ans=0;
while(bfs())
cur_ans+=dfs(0,(int)1e9);
return cur_ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("ce.in","r",stdin);
#endif
scanf("%d%d",&m,&n);
int u,v;
scanf("%d%d",&u,&v);
for(;;)
{
if(u==-1&&v==-1) break;
if(u>v) swap(u,v);
add(u,v,1);
scanf("%d%d",&u,&v);
}
for(int i=1;i<=m;i++) add(0,i,1);
for(int i=m+1;i<=n;i++) add(i,n+1,1);
int ans=dinic();
if(ans==0) {printf("No Solution!\n");return 0;}
printf("%d\n",ans);
for(int i=m+1;i<=n;i++)
{
for(int j=head[i];j;j=edge[j].nxt)
{
if(edge[j].dis!=0&&edge[j].to!=n+1)
printf("%d %d\n",edge[j].to,i);
}
}
return 0;
}
标签:false swa show space struct pop can max online
原文地址:https://www.cnblogs.com/fengxunling/p/10290588.html