题目:https://www.acwing.com/problem/content/2177/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int N=200+10,M=(110*110+210)*2,inf=1e8;
int h[N],ver[M],edge[M],ne[M],tot;
int m,n,s,t,maxflow;
int d[N],cur[N];
void add(int x,int y,int z)
{
ver[tot]=y;edge[tot]=z;ne[tot]=h[x];h[x]=tot++;
ver[tot]=x;edge[tot]=0;ne[tot]=h[y];h[y]=tot++;
}
bool bfs()
{
memset(d,-1,sizeof d);
queue<int> q;
q.push(s);d[s]=0;cur[s]=h[s];
while(q.size())
{
int x=q.front();q.pop();
for(int i=h[x];i!=-1;i=ne[i])
{
int y=ver[i];
if(d[y]==-1&&edge[i])
{
d[y]=d[x]+1;
cur[y]=h[y];
q.push(y);
if(y==t) return 1;
}
}
}
return 0;
}
int dinic(int x,int limit)
{
if(x==t) return limit;
int flow=0;
for(int i=cur[x];i!=-1&&flow<limit;i=ne[i])
{
cur[x]=i;
int y=ver[i];
if(edge[i]&&d[y]==d[x]+1)
{
int k=dinic(y,min(limit-flow,edge[i]));
if(!k) d[y]=-1;
flow+=k;
edge[i]-=k;
edge[i^1]+=k;
}
}
return flow;
}
int main()
{
memset(h,-1,sizeof h);
scanf("%d%d",&m,&n);
int x,y;
s=n+1;t=n+2;
while(scanf("%d%d",&x,&y)&&x!=-1)
{
add(x,y,1);
}
for(int i=1;i<=m;i++)
{
add(s,i,1);
}
for(int i=m+1;i<=n;i++)
{
add(i,t,1);
}
int flow;
while(bfs())
{
while(flow=dinic(s,inf))
{
maxflow+=flow;
}
}
printf("%d\n",maxflow);
for(int i=0;i<tot;i+=2)
{
if(ver[i]>m&&ver[i]<=n&&edge[i]==0)
{
printf("%d %d\n",ver[i^1],ver[i]);
}
}
}