ACM-ICPC Live Archive Regionals 2014 >> Asia - Kuala Lumpur 6811 - Irrigation Lines

参见:转载前页面

怎么读题时就没发现“每行每列各有一个水阀”?这是把问题转化成二分图模型的关键啊

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#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;
}