您的位置:首页 > 其它

poj 1274(二分图最大匹配)

2014-02-20 23:07 381 查看
题意:题目就是求二分图最大匹配。

思路:直接使用匈牙利算法,主要还是用来熟悉一下模版。

代码如下:

/**************************************************
* Author     : xiaohao Z
* Blog     : http://www.cnblogs.com/shu-xiaohao/ * Last modified : 2014-02-20 22:44
* Filename     : poj_1274.cpp
* Description     :
* ************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define MP(a, b) make_pair(a, b)
#define PB(a) push_back(a)

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<unsigned int,unsigned int> puu;
typedef pair<int, double> pid;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

const int INF = 0x3f3f3f3f;
const double eps = 1E-6;
const int LEN = 1010;
vector<int> Map[LEN];
int xn, yn, n, vis[LEN], match[LEN];

bool dfs(int v){
vis[v] = 1;
for(int i=0; i<Map[v].size(); i++){
int u = Map[v][i], w = match[u];
if(w < 0 || (!vis[w] && dfs(w))){
match[v] = u;
match[u] = v;
return true;
}
}
return false;
}

int hungary(){
int ret = 0;
memset(match, -1, sizeof match);
for(int i=0; i<n; i++){
if(match[i] < 0){
memset(vis, 0, sizeof vis);
if(dfs(i)) ret ++;
}
}
return ret;
}

int main()
{
//    freopen("in.txt", "r", stdin);

while(scanf("%d%d", &xn, &yn)!=EOF){
for(int i=0; i<LEN; i++) Map[i].clear();
for(int i=0; i<xn; i++){
int tn, b;
scanf("%d", &tn);
for(int j=0; j<tn; j++){
scanf("%d", &b);b--;
Map[i].PB(b+xn);
Map[b+xn].PB(i);
}
}
n = xn + yn;
int ans = hungary();
printf("%d\n", ans);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: