POJ 3041 Asteroids

解题思路:将行列分别对应二部图的两个set(行,列)->(left, right),问题转化为最小顶点覆盖问题,进而转化为求解最大流问题

  
    
#include < iostream >
using namespace std;
#define Num 505

bool edge[Num][Num];
int father[Num];
bool visit[Num];
int N;

int Search( int x)
{
for ( int i = 1 ; i <= N; i ++ )
{
if ( ! visit[i] && edge[x][i])
{
visit[i]
= true ;
if (father[i] == 0 || Search(father[i]))
{
father[i]
= x;
return 1 ;
}
}
}
return 0 ;
}

int main()
{
int K, R, C, t = 0 ;
scanf(
" %d%d " , & N, & K);
for ( int i = 0 ; i < K; i ++ )
{
scanf(
" %d%d " , & R, & C);
edge[R][C]
= true ;
}
for ( int i = 1 ; i <= N; i ++ )
{
memset(visit,
0 , sizeof (visit));
t
+= Search(i);
}
printf(
" %d\n " , t);
return 0 ;
}

 

你可能感兴趣的:(poj)