您的位置:首页 > 其它

bzoj1191: [HNOI2006]超级英雄Hero(二分图匹配)

2017-09-25 20:50 323 查看
题目传送门

这道题唯一的好处就是提醒我们仔细读题。。。。

一眼看出二分图匹配。

心里很开心以为碰到了一道水题。

水了一发结果错了。

错了很久。哈哈哈。。

无奈上网看看题解。

发现一找不到答案的话就要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;
}


水一发!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: