​题目​

分析

裸匈牙利算法
存储:邻接表+dfs


#include <bits/stdc++.h>
#pragma GCC diagnostic error "-std=c++11"
#define d(x) cout << (x) << endl
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e2 + 10;

int n, m, q, p;
vector<int> g[N];
int vis[N]; //Y点集是否访问过
int link[N]; //linl[y]=x Y点集与x连接

int find(int x)
{
for(auto y : g[x]){
if(!vis[y]){
vis[y] = 1;
if(link[y] == -1 || find(link[y])){
link[y] = x;
return 1;
}
}
}
return 0;
}

int main()
{
scanf("%d%d", &m, &n);
while(~scanf("%d%d", &p, &q)){
if(q == -1 && p == -1)
break;
g[p].push_back(q);
g[q].push_back(p);
}
memset(link, -1, sizeof(link));
int ans = 0;
for (int i = 1; i <= m; i++){
memset(vis, 0, sizeof(vis));
if(find(i))
ans++;
}
printf("%d\n", ans);
return 0;
}