参考:
https://blog.csdn.net/cillyb/article/details/55511666
https://blog.csdn.net/c20180630/article/details/70175814
模板:
#include <bits/stdc++.h> using namespace std; const int maxN = 1e5 + 7; vector<int> G[maxN]; int match[maxN]; int vis[maxN]; int n, m, sum; int dfs(int u) { for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; //有路而且没被访问 if(!vis[v]) { vis[v] = 1;//标记点i已经访问过 //如果点i未被配对或者找到了新的配对 if(match[v] == 0 || dfs(match[v])) { //更新配对关系 match[v] = u; match[u] = v; return 1; } } } return 0; } int main() { // freopen("1.txt","r", stdin); scanf("%d %d", &n, &m); for(int i = 1; i <= n; i++) { int u, v; scanf("%d %d", &u, &v); G[u].push_back(v); G[v].push_back(u); } memset(match, 0 , sizeof(match)); for(int i = 1; i <= n; i++){ memset(vis, 0, sizeof(vis)); if(dfs(i)) { sum++; } } printf("%d ", sum); }