https://blog.csdn.net/ivy_uu/article/details/52091873
https://zhuanlan.zhihu.com/p/89972891
匈牙利:
4 8
1 5
1 6
2 5
3 5
3 7
4 5
4 7
4 8
-1 -1
out:4
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; typedef long long LL; int n,m,line[105][105],have[105],vis[105];/////
//匈牙利:我占了你的,你去找其他出路 int work(int u)//返回的是该点能找见匹配点 { for(int v=m+1;v<=n;v++) { if(line[u][v]==1 && !vis[v])////在这一趟中不来回找 { vis[v]=1; if(!have[v] || work(have[v])==1)//have[]表示匹配点匹配上的发出点(左) { have[v]=u; return 1; } } } return 0; } int x,y,cnt; int main() { scanf("%d%d",&m,&n); scanf("%d%d",&x,&y);//x是外籍飞行员1~m, y是皇家飞行员m+1~n while(x!=-1 && y!=-1 ) { line[x][y]=1; scanf("%d%d",&x,&y); } memset(have,0,sizeof have); for(int i=1;i<=m;i++) { memset(vis,0,sizeof vis); if(work(i)==1) cnt++; } printf("%d\n",cnt); if(cnt==0) {cout<<"No Solution!"<<endl;return 0;} for(int i=m+1;i<=n;i++) { if(have[i]>0) printf("%d %d\n",have[i],i); } return 0; }