过山车
原题链接:传送门
二分图最大匹配模板题,但sb了数组开小了一直TLE,虽然是模板但很长教训。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int k, n, m;
const int N = 550, M = 40100;
int match[N];
int h[N], ne[M], e[M], idx;
bool vis[N];
void add(int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
bool dfs(int x){
for(int i = h[x]; i != -1; i = ne[i]){
int j = e[i];
if(!vis[j]){
vis[j] = true;
if(match[j] == 0 || dfs(match[j])){
match[j] = x;
return true;
}
}
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
while(cin >> k && k){
cin >> n >> m;
memset(h, -1, sizeof h);
memset(match, 0, sizeof match);
while(k--){
int x, y;
cin >> x >> y;
add(x, y);
}
int res = 0;
for(int i = 1; i <= n; i ++){
memset(vis, 0, sizeof vis);
if(dfs(i)) res++;
}
cout << res << "\n";
}
return 0;
}