您的位置:首页 > 其它

poj 2239 Selecting Courses 最大二分匹配

2011-01-29 21:44 302 查看
把p,q化为一维之后,直接用匈牙利算法

#include<iostream>
using namespace std;

const int MAX = 200;

bool arcs[MAX][MAX];
int match[MAX];
bool isvisit[MAX];
int n, m = 7*12;

int find(int u)
{
for (int i = 1; i <= m; i++)
if (arcs[u][i] && !isvisit[i])
{
isvisit[i] = true;
if (!match[i] || find(match[i]))
{
match[i] = u;
return true;
}
}

return false;
}
int main()
{
int t, p, q;

while (cin >> n)
{
memset(arcs, false, sizeof(arcs));
memset(match, 0, sizeof(match));

for (int j = 1; j <= n; j++)
{
cin >> t;
for (int i = 1; i <= t; i++)
{
cin >> p >> q;
arcs[j][12*(p-1) + q] = true;
}
}

int ans = 0;
for (int i = 1; i <= n; i++)
{
memset(isvisit, false, sizeof(isvisit));
if (find(i))
ans++;
}

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