过山车
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19256 Accepted Submission(s): 8415
Problem Description
Input
1<=N 和M<=500.接下来的K行,每行有两个数,分别表示女生Ai愿意和男生Bj做partner。最后一个0结束输入。
Output
Sample Input
Sample Output
Author
二分图入门题型:二分图讲解
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 505;
int line[N][N];//对上眼的组合
int gril[N];
bool book[N];//标记已经匹配的女生
int k, m, n;
//为男生匹配女生,递归法实现
//匈牙利算法核心
bool found(int x) {
for (int i = 1; i <= n; ++i) {
if (line[x][i] && !book[i]) {
book[i] = true;
if (!gril[i] || found(gril[i])) {
book[i] = true;
gril[i] = x;
return true;
}
}
}
return false;
}
int main() {
int x, y;
while (cin >> k && k) {
cin >> m >> n;
memset(line, 0, sizeof(line));
memset(gril, 0, sizeof(gril));
for (int i = 0; i < k; ++i) {
cin >> x >> y;
line[x][y] = 1;//男生女生对上电波了
}
int sum = 0;//计算最大撮合数
for (int i = 1; i <= m; ++i) {
memset(book, false, sizeof(book));
if (found(i))++sum;
}
cout << sum << endl;
}
return 0;
}
posted @
2020-04-25 20:59
RioTian
阅读(...)
评论(...)
编辑
收藏