BoxerTC

Untitled

Mar 15th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define sc( x ) scanf( "%d" , &x )
  5. #define REP( i , n ) for( int i = 0 ; i < n ; ++i )
  6. #define clr( t , val ) memset( t , val , sizeof( t ) )
  7.  
  8. #define pb push_back
  9. #define all( v ) v.begin() , v.end()
  10. #define SZ( v ) ((int)(v).size())
  11.  
  12. #define mp make_pair
  13. #define fi first
  14. #define se second
  15.  
  16. #define N 15
  17.  
  18. typedef vector< int > vi;
  19. typedef pair< int , int > pii;
  20. typedef long long ll;
  21.  
  22. int n , K;
  23. int B[ N + 5 ][ N + 5 ] , W[ N + 5 ][ N + 5 ];
  24. int outA[ N + 5 ] , outB[ N + 5 ];
  25. bool DP[ N + 5 ][ 1 << N ];
  26. int vis[ N + 5 ][ 1 << N ];
  27.  
  28. bool f( int kk ){
  29.     K = kk;
  30.     clr( DP , 1 );
  31.     REP( it , n + n ){
  32.         queue< pii > Q;
  33.         clr( vis , 0 );
  34.         REP( i , n )
  35.             REP( mask , 1 << K ){
  36.                 if( outA[ i ] == 0 || outB[ i ] == 0 || DP[ i ][ mask ] == 0 ){
  37.                     REP( mask , 1 << K ){
  38.                         Q.push( mp( i , mask ) );
  39.                         DP[ i ][ mask ] = 0;
  40.                         vis[ i ][ mask ] = 1;
  41.                     }
  42.                 }
  43.             }
  44.        
  45.         while( !Q.empty() ){
  46.             pii V = Q.front();
  47.             Q.pop();
  48.             int v = V.fi , to = V.se;
  49.            
  50.             REP( u , n ){
  51.                 if( !B[ u ][ v ] ) continue;
  52.                 bool ok = 1;
  53.                 REP( w , n ) if( B[ u ][ w ] ){
  54.                     if( DP[ w ][ to ] && DP[ w ][ to ^ (1<<(K - 1)) ] ){
  55.                         ok = 0;
  56.                         break;
  57.                     }
  58.                 }
  59.                 if( ok ){
  60.                     int mask = to;
  61.                     if( mask & (1<<(K - 1)) ) mask -= (1<<(K - 1));
  62.                     mask <<= 1;
  63.                     if( !vis[ u ][ mask ] ){
  64.                         vis[ u ][ mask ] = 1;
  65.                         DP[ u ][ mask ] = 0;
  66.                         Q.push( mp( u , mask ) );
  67.                     }
  68.                 }
  69.             }
  70.            
  71.             REP( u , n ){
  72.                 if( !W[ u ][ v ] ) continue;
  73.                 bool ok = 1;
  74.                 REP( w , n ) if( W[ u ][ w ] ){
  75.                     if( DP[ w ][ to ] && DP[ w ][ to ^ (1<<(K - 1)) ] ){
  76.                         ok = 0;
  77.                         break;
  78.                     }
  79.                 }
  80.                 if( ok ){
  81.                     int mask = to;
  82.                     if( mask & (1<<(K - 1)) ) mask -= (1<<(K - 1));
  83.                     mask <<= 1;
  84.                     mask ++;
  85.                     if( !vis[ u ][ mask ] ){
  86.                         vis[ u ][ mask ] = 1;
  87.                         DP[ u ][ mask ] = 0;
  88.                         Q.push( mp( u , mask ) );
  89.                     }
  90.                 }
  91.             }
  92.            
  93.         }
  94.     }
  95.     bool ok = 1;
  96.     REP( mask , 1 << K ) if( !DP[ 0 ][ mask ] ) ok = 0;
  97.     return ok;
  98. }
  99.  
  100. int main(){
  101.     int cases;
  102.     sc( cases );
  103.     REP( tc , cases ){
  104.         sc( n );
  105.         clr( outA , 0 );
  106.         clr( outB , 0 );
  107.         REP( i , n ) REP( j , n ) {
  108.             int x;
  109.             sc( x );
  110.             B[ i ][ j ] = x;
  111.             if( x == 1 ) outA[ i ] ++;
  112.         }
  113.         REP( i , n ) REP( j , n ){
  114.             int x;
  115.             sc( x );
  116.             W[ i ][ j ] = x;
  117.             if( x == 1 ) outB[ i ] ++;
  118.         }
  119.         for( int i = 1 ; i <= 10 ; ++i ) cout << f( i ) << " ";
  120.         cout << endl;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment