思路:裸的匈牙利算法模板。这位大佬写的简单易懂,可以参考一下二分图匹配问题。https://blog.csdn.net/dark_scope/article/details/8880547
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 105; int graph[maxn][maxn]; int used[maxn]; int match[maxn]; int n,m; int solve(int x) { for(int i = 0;i < m;i++) { if(graph[x][i]&&!used[i]) { used[i] = 1; if(match[i]==-1||solve(match[i])>0) { match[i] = x; return 1; } } } return 0; } int main() { int ans = 0; cin>>n>>m; int a,b; while(cin>>a>>b) { if(a == -1||b == -1) break; graph[a-1][b-1] = 1; } memset(match,-1,sizeof(match)); for(int i = 0;i < n;i++) { memset(used,0,sizeof(used)); if(solve(i)) ans++; } if(ans > 0)cout<<ans<<endl; else cout<<"No Solution!"<<endl; return 0; }