#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAX 200
#define MAXL 200*200
using namespace std;
struct Line{
int v,next;
}e[MAXL];
int h[MAX],cnt=0,sum=0,match[MAX],n,m,u,v;
bool vis[MAX];
void addedge(int u,int v){
e[cnt]=(Line){v,h[u]};
h[u]=cnt++;
}
bool dfs(int u){
for(int i=h[u];i!=-1;i=e[i].next){
int v=e[i].v;
if(!vis[v]){
vis[v]=true;
if(!match[v] or dfs(match[v])){
match[v]=u;
return 1;
}
}
}
return 0;
}
int main(){
memset(h,-1,sizeof(h));
scanf("%d%d",&n,&m);
while(1){
scanf("%d%d",&u,&v);
if(u==-1)break;
addedge(u,v);
}
for(int i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
if(dfs(i))sum++;
}
cout<<sum<<endl;
for(int i=n+1;i<=m;i++){
if(match[i]){
printf("%d %d\n",match[i],i);
}
}
return 0;
}
原文:https://www.cnblogs.com/ezoihy/p/8893815.html