[算法] 匈牙利算法 poj 1274 示例 [ 二分图匹配 入门篇 ] O(n*m) Hopcroft O(sqrt(n)*m)

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
#include <functional>
#include <utility>
#include <sstream>
#include <climits>
#include <cassert>
#define BUG puts("here!!!");

using namespace std;
const int N = 205;
vector<int> p[N];
int mat[N];
bool vis[N];
int n, m;
int ca, e;
bool dfs(int u) {
	for(int i = 0; i < p[u].size(); i++) {
		int to = p[u][i];
		if(!vis[to]) {
			vis[to] = true;
			if(mat[to] == 0 || dfs(mat[to])) {
				mat[to] = u;
				return true;
			}
		}
	}
	return false;
}
int Maxmatch() {
	int num = 0;
	memset(mat, 0, sizeof(mat));
	for(int i = 1; i <= n; i++) {
		memset(vis, 0, sizeof(vis));
		if(dfs(i)) num++;
	}
	return num;
}
int main() {
	while(scanf("%d%d", &n, &m) == 2) {
		for(int i = 0; i <= n; i++) {
			p[i].clear();
		}
		for(int i = 1; i <= n; i++) {
			scanf("%d", &ca);
			while(ca--) {
				scanf("%d", &e);
				p[i].push_back(e);
			}
		}
		printf("%d\n", Maxmatch());
	}
	return 0;
}

posted @ 2013-01-19 17:35  小尼人00  阅读(201)  评论(0编辑  收藏  举报