用匈牙利就行,比较赤裸。
/* * hdu2119/win.cpp * Created on: 2012-8-13 * Author : ben */ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MAXN = 210; int N, M, mymatch[MAXN]; bool visited[MAXN], mymap[MAXN][MAXN]; bool buildgraph() { int t; scanf("%d", &N); if(N == 0) { return false; } scanf("%d", &M); memset(mymap, false, sizeof(mymap)); for(int i = 0; i < N; i++) { for(int j = 0; j < M; j++) { scanf("%d", &t); if(t == 1) { mymap[i][j] = true; } } } return true; } bool dfs(int k) { int t; for (int i = 0; i < M; i++) { if (mymap[k][i] && !visited[i]) { visited[i] = true; t = mymatch[i]; mymatch[i] = k; if (t == -1 || dfs(t)) { return true; } mymatch[i] = t; } } return false; } int hungary () { memset(mymatch, -1, sizeof(mymatch)); int ans = 0; for (int i = 0; i < N; i++) { memset(visited, false, sizeof(visited)); if (dfs(i)) { ans++; } } return ans; } int main() { #ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); #endif while(buildgraph()) { printf("%d\n", hungary()); } return 0; }