acwing861 二分图的最大匹配数
- 问题
发现没有人询问为什么回溯的时候st[j]不要置为false,既然我选不到这个妹子了,为什么我还捏着不放? - 原因是:
其实放手也行(st[j] = false),这样会让其他人继续撬这个妹子的墙角,但同样也撬不动。
浪费时间,所以为了节省时间,让后人放弃撬这个妹子墙角。
总结:剪枝节省时间
例题
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 505, M = 2e5+7;
int h[N], e[M], ne[M], idx;
int match[N];
bool st[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
int n1, n2, m;
bool find(int x) {
for(int i = h[x]; ~i; i = ne[i]) {
int j = e[i];
if(st[j]) continue;
st[j] = true;
if(!match[j] or find(match[j])) {
match[j] = x;
return true;
}
st[j] = false;
}
return false;
}
int main()
{
cin >> n1 >> n2 >> m;
memset(h, -1, sizeof h);
for(int i = 0 ;i < m; ++i) {
int a, b;
cin >> a >> b;
add(a, b);
}
int res = 0;
for(int i = 1; i <= n1; ++i) {
memset(st, false, sizeof st);
if(find(i)) ++res;
}
cout << res;
return 0;
}
作者:Luci
链接:https:
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。