poj 3041
二分图匹配
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int MAX_N = 505;
const int MAX_K = 10005;
int n, k, V;
int r[MAX_K], c[MAX_K];
int match[MAX_K];
bool used[MAX_K];
vector<int> G[MAX_K];
void add_edge(int u, int v) {
G[u].push_back(v);
G[v].push_back(u);
}
bool dfs(int v) {
used[v] = true;
for(int i=0; i<G[v].size(); i++) {
int u = G[v][i], w = match[u];
if(w<0 || !used[w] && dfs(w)) {
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}
int bipartite_matching() {
int res = 0;
memset(match, -1, sizeof(match));
for(int v=0; v<V; v++) {
if(match[v] < 0) {
memset(used, 0, sizeof(used));
if(dfs(v))
res++;
}
}
return res;
}
void solve() {
V = 2 * n;
for(int i=0; i<k; i++)
add_edge(r[i]-1, n+c[i]-1);
printf("%d\n", bipartite_matching());
}
int main() {
scanf("%d%d", &n, &k);
for(int i=0; i<k; i++)
scanf("%d%d", &r[i], &c[i]);
solve();
return 0;
}

浙公网安备 33010602011771号