BZOJ大视野 1059: [ZJOI2007]矩阵游戏 解题报告

二分图匹配,用行匹配列。

code:

/**************************************************************
     Problem: 1059
     Language: C++
     Result: Accepted
     Time:316 ms
     Memory:1744 kb
****************************************************************/
  
// Forever_LF
#include
#include
#include
using namespace std;
  
struct node
{
     int x,y,next;
}a[40001]; int len,first[201];
int L[201];
bool ev[201];
  
void ins( int x, int y )
{
     len++;
     a[len].x=x; a[len].y=y; 
     a[len].next = first[x]; first[x] = len;
}
bool ex( int x )
{
     for ( int k=first[x];k;k=a[k].next )
     {
         int y=a[k].y;
         if ( ev[y] ) continue ;
         ev[y] = true ;
         if ( !L[y] || ex( L[y] ) )
         {
             L[y] = x;
             return true ;
         }
     }
     return false ;
}
  
int main()
{
     int n,m,t,i,j,l;
     bool v;
     scanf ( "%d" ,&m);
     while ( m-- )
     {
         memset ( first,0, sizeof first );
         memset ( L,0, sizeof L );
         len = 0; v = true ;
          
         scanf ( "%d" ,&n);
         for ( i=1;i<=n;i++ )
             for ( j=1;j<=n;j++ )
             {
                 scanf ( "%d" ,&l);
                 if ( l ) ins( i,j );
             }
         for ( i=1;i<=n;i++ )
         {
             memset ( ev, false , sizeof ev );
             if ( !ex( i ) )
             {
                 v = false
                 break ;
             }
         }
         if ( v ) printf ( "Yes\n" );
         else printf ( "No\n" );
     }
     return 0;
}

 

你可能感兴趣的:(二分图匹配,BZOJ)