怎么读题时就没意识到“每行每列各有一个水阀”?这是把问题转化成二分图模型的关键啊
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4823
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4823
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN = 110;
char graph[MAXN][MAXN];
bool visited[MAXN];
int nCase, cCase, use[MAXN], m, n;
void init() {
memset(use, -1, sizeof(use));
}
void input() {
scanf("%d%d", &m, &n);
for (int i = 0; i < m; i++) {
scanf("%s", graph[i]);
}
}
bool find(int x) {
for (int j = 0; j < n; j++) {
if (graph[x][j] == '1' && !visited[j]) {
visited[j] = true;
if (use[j] == -1 || find(use[j])) {
use[j] = x;
return true;
}
}
}
return false;
}
int match() {
int count = 0;
for (int i = 0; i < m; i++) {
memset(visited, false, sizeof(visited));
if (find(i)) count++;
}
return count;
}
void solve() {
printf("Case #%d: %d\n", ++cCase, match());
}
int main() {
scanf("%d", &nCase);
while (nCase--) {
init();
input();
solve();
}
return 0;
}