Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define sc( x ) scanf( "%d" , &x )
- #define REP( i , n ) for( int i = 0 ; i < n ; ++i )
- #define clr( t , val ) memset( t , val , sizeof( t ) )
- #define pb push_back
- #define all( v ) v.begin() , v.end()
- #define SZ( v ) ((int)(v).size())
- #define mp make_pair
- #define fi first
- #define se second
- #define N 15
- typedef vector< int > vi;
- typedef pair< int , int > pii;
- typedef long long ll;
- int n , K;
- int B[ N + 5 ][ N + 5 ] , W[ N + 5 ][ N + 5 ];
- int outA[ N + 5 ] , outB[ N + 5 ];
- bool DP[ N + 5 ][ 1 << N ];
- int vis[ N + 5 ][ 1 << N ];
- bool f( int kk ){
- K = kk;
- clr( DP , 1 );
- REP( it , n + n ){
- queue< pii > Q;
- clr( vis , 0 );
- REP( i , n )
- REP( mask , 1 << K ){
- if( outA[ i ] == 0 || outB[ i ] == 0 || DP[ i ][ mask ] == 0 ){
- REP( mask , 1 << K ){
- Q.push( mp( i , mask ) );
- DP[ i ][ mask ] = 0;
- vis[ i ][ mask ] = 1;
- }
- }
- }
- while( !Q.empty() ){
- pii V = Q.front();
- Q.pop();
- int v = V.fi , to = V.se;
- REP( u , n ){
- if( !B[ u ][ v ] ) continue;
- bool ok = 1;
- REP( w , n ) if( B[ u ][ w ] ){
- if( DP[ w ][ to ] && DP[ w ][ to ^ (1<<(K - 1)) ] ){
- ok = 0;
- break;
- }
- }
- if( ok ){
- int mask = to;
- if( mask & (1<<(K - 1)) ) mask -= (1<<(K - 1));
- mask <<= 1;
- if( !vis[ u ][ mask ] ){
- vis[ u ][ mask ] = 1;
- DP[ u ][ mask ] = 0;
- Q.push( mp( u , mask ) );
- }
- }
- }
- REP( u , n ){
- if( !W[ u ][ v ] ) continue;
- bool ok = 1;
- REP( w , n ) if( W[ u ][ w ] ){
- if( DP[ w ][ to ] && DP[ w ][ to ^ (1<<(K - 1)) ] ){
- ok = 0;
- break;
- }
- }
- if( ok ){
- int mask = to;
- if( mask & (1<<(K - 1)) ) mask -= (1<<(K - 1));
- mask <<= 1;
- mask ++;
- if( !vis[ u ][ mask ] ){
- vis[ u ][ mask ] = 1;
- DP[ u ][ mask ] = 0;
- Q.push( mp( u , mask ) );
- }
- }
- }
- }
- }
- bool ok = 1;
- REP( mask , 1 << K ) if( !DP[ 0 ][ mask ] ) ok = 0;
- return ok;
- }
- int main(){
- int cases;
- sc( cases );
- REP( tc , cases ){
- sc( n );
- clr( outA , 0 );
- clr( outB , 0 );
- REP( i , n ) REP( j , n ) {
- int x;
- sc( x );
- B[ i ][ j ] = x;
- if( x == 1 ) outA[ i ] ++;
- }
- REP( i , n ) REP( j , n ){
- int x;
- sc( x );
- W[ i ][ j ] = x;
- if( x == 1 ) outB[ i ] ++;
- }
- for( int i = 1 ; i <= 10 ; ++i ) cout << f( i ) << " ";
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment