https://www.luogu.org/problemnew/show/P2756
裸的二分图最大匹配,用dinic
#include <bits/stdc++.h>
using namespace std;
const int maxn=205;
const int INF=0x3f3f3f3f;
struct Edge{
int from,to,cap,flow;
};
struct Dinic{
int n1,n2,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init()
{
int a,b;
cin>>n1>>n2;
s=0;t=n1+n2+1;
while(scanf("%d%d",&a,&b) && a!=-1)AddEdge(a,b,1);
for(int i=1;i<=n1;i++)AddEdge(s,i,1);
for(int i=n1+1;i<=n1+n2;i++)AddEdge(i,t,1);
}
void AddEdge(int f,int t,int c)
{
edges.push_back((Edge){f,t,c,0});
edges.push_back((Edge){t,f,0,0});
m=edges.size();
G[f].push_back(m-2);
G[t].push_back(m-1);
}
bool bfs()
{
memset(vis,0,sizeof(vis));
queue<int> Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty())
{
int x=Q.front();Q.pop();
for(int i=0;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t || a==0)return a;
int flow=0,f;
for(int& i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to] && (f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(!a)break;
}
}
return flow;
}
int MaxFlow()
{
int flow=0;
while(bfs())
{
memset(cur,0,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
void print()
{
for(int i=0;edges[i].from!=s;i+=2)
{
if(edges[i].flow)printf("%d %d\n",edges[i].from,edges[i].to);
}
}
}ans;
int main()
{
//freopen("input.in","r",stdin);
ans.init();
cout<<ans.MaxFlow()<<"\n";
ans.print();
return 0;
}