题目传送门
这道题唯一的好处就是提醒我们仔细读题。。。。
一眼看出二分图匹配。
心里很开心以为碰到了一道水题。
水了一发结果错了。
错了很久。哈哈哈。。
无奈上网看看题解。
发现一找不到答案的话就要break。
然后又回去仔细读了一遍题,发现错一题就不能继续了。
哈哈哈[苦逼]
就是二分图匹配了。。
没什么好说的。
代码实现:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
bool map[1100][1100];
int match[1100];
bool chw[1100];int n,m;
bool get_match(int x) {
for(int i=1;i<=n;i++) {
if(map[x][i]==true) {
if(chw[i]==false) {
chw[i]=true;
if(match[i]==0||get_match(match[i])==true) {
match[i]=x;return true;
}
}
}
}
return false;
}
int main() {
scanf("%d%d",&n,&m);
memset(map,false,sizeof(map));
for(int i=1;i<=m;i++) {
int x,y;scanf("%d%d",&x,&y);
x++;y++;
map[i][x]=true;map[i][y]=true; //关系匹配
}
int ans=0;
memset(match,0,sizeof(match));
for(int i=1;i<=m;i++) {
memset(chw,false,sizeof(chw));
if(get_match(i)==true)
ans++;
else
break;
}
printf("%d\n",ans);
return 0;
}
水一发!

本文通过一道编程题解析了二分图匹配的实现过程及注意事项。作者分享了从初次尝试失败到阅读题解最终解决问题的心路历程,并附上了完整的代码实现。
&spm=1001.2101.3001.5002&articleId=78089311&d=1&t=3&u=64f2deb21eb74bbfa4b6d5abf25fc44c)
1093

被折叠的 条评论
为什么被折叠?



