poj 1466 Girls and Boys

这是一道求最大独立集的问题;

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<queue>

#include<set>

#include<map>

#include<cstring>

#include<vector>

using namespace std;

bool G[524][524],visit[524];

int match[524];

bool path( int num , int N )

{

     for( int i = 1 ; i <= N ; i++ )

     {

         if( !visit[i] && G[num][i] )

         {

             visit[i] = true;

             if( match[i]==0 || path( match[i] , N ) )

             {

                 match[i] = num;

                 return true;        

             }        

         }        

     }    

     return false;

}

int main(  )

{

    int N,n,num,m;

    while( scanf( "%d",&N )==1 )

    {

       memset( G ,0 , sizeof( G )  );

       memset( match , 0 , sizeof( match ) );

       for( int i = 1 ; i <= N ; i ++ )

       {

           scanf( "%d: (%d)",&num , &n );

           for( int j = 0 ; j < n ; j ++ )

           {

                scanf( "%d",&m );

                G[num+1][m+1] = true;        

           }         

       }

       int ans = 0;

       for( int i = 1; i <= N; i++ )

       {

           memset( visit , 0 , sizeof( visit ) );

           if( path( i , N ) ) ans++;        

       }

       printf( "%d\n",N - ans/2 );

    }

    //system( "pause" );

    return 0;

}

 

你可能感兴趣的:(poj)