pku 3041 Asteroids 二分图匹配——匈牙利算法求最小点覆盖

http://poj.org/problem?id=3041

将行与列转化为左右点集,只要出现Asteroids的点(i,j)就对i j建边,然后求最小点覆盖即可。。。

View Code
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 507
using namespace std;
int map[maxn][maxn];
int link[maxn];
bool vt[maxn];
int n,m;
bool dfs(int rpos)
{
int i;
for (i = 1; i <= n; ++i)
{
if (!vt[i] && map[rpos][i])
{
vt[i] = true;
if (link[i] == -1 || dfs(link[i]))
{
link[i] = rpos;
return true;
}
}
}
return false;
}
void init()
{
int i,j;
for (i = 0; i <= n; ++i)
{
link[i] = -1;
for (j = 0; j <= n; ++j)
map[i][j] = 0;
}
}
int main()
{
int i,x,y;
cin>>n>>m;
init();
for (i = 0; i < m; ++i)
{
cin>>x>>y;
map[x][y] = 1;
}
int count = 0;
for (i = 1; i <= n; ++i)
{
memset(vt,false,sizeof(vt));
if (dfs(i)) count++;
}
cout<<count<<endl;
return 0;
}



你可能感兴趣的:(二分图)