思路:直接用匈牙利算法的板子
参考博客:
https://blog.csdn.net/C20180630/article/details/70175814
https://blog.csdn.net/Dark_Scope/article/details/8880547
邻接表代码:
#include<bits/stdc++.h>
#define maxn 100000
using namespace std;
struct node{
int to,next;
}edge[maxn];
int k,m,n,tot;
int linker[maxn],head[maxn];
bool vis[maxn];
void init(){
tot=0;
for(int i=0;i<maxn;i++){
edge[i].next=-1;
head[i]=-1;
linker[i]=-1;
}
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
bool dfs(int x){
for(int i=head[x];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(!vis[v]){
vis[v]=true;
if(linker[v]==-1||dfs(linker[v])){
linker[v]=x;
return true;
}
}
}
return false;
}
int main(){
while(~scanf("%d",&k),k){
init();
scanf("%d%d",&m,&n);
for(int i=1;i<=k;i++){
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
int sum=0;
for(int i=1;i<=m;i++){//为每个女孩配对
memset(vis,false,sizeof vis);//每次都要初始化
if(dfs(i)){
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}