传送门:【HDU】2828 Lamp
题目分析:重复覆盖倒是很好想。。
然后因为看到HH博客园上的文章说可以精确覆盖。。。然后就纠结精确覆盖该怎么弄。。。。趴在桌上想啊想。。。不小心就睡着了。。醒来后决定还是敲一个重复覆盖的了。。。
本题重复覆盖,列为灯,行为开关,开关拆成两个,开一个状态,关一个状态,各一行。选择的时候要保证每个状态只能选一次。其他貌似没什么了。。。。
代码如下:
#include <cmath> #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; #define REP( i , a , b ) for ( int i = a ; i < b ; ++ i ) #define REV( i , a , b ) for ( int i = a - 1 ; i >= b ; -- i ) #define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define FOV( i , a , b ) for ( int i = a ; i >= b ; -- i ) #define CLR( a , x ) memset ( a , x , sizeof a ) #define REC( i , A , o ) for ( int i = A[o] ; i != o ; i = A[i] ) const int MAXC = 1005 ; const int MAXR = 1005 ; const int MAXNODE = 1000000 ; const int INF = 0x3f3f3f3f ; struct DLX { int L[MAXNODE] , R[MAXNODE] , U[MAXNODE] , D[MAXNODE] ; int row[MAXNODE] , col[MAXNODE] ; int H[MAXR] , S[MAXC] ; int size ; int nv ; int n , m ; bool vis[MAXR] ; void init () { CLR ( H , -1 ) ; FOR ( i , 0 , nv ) { S[i] = 0 ; U[i] = D[i] = i ; L[i] = i - 1 ; R[i] = i + 1 ; } L[0] = nv ; R[nv] = 0 ; size = nv ; } void link ( int r , int c ) { ++ size ; ++ S[c] ; row[size] = r ; col[size] = c ; U[size] = U[c] ; D[size] = c ; D[U[c]] = size ; U[c] = size ; if ( ~H[r] ) { L[size] = L[H[r]] ; R[size] = H[r] ; L[R[size]] = size ; R[L[size]] = size ; } else H[r] = L[size] = R[size] = size ; } void remove ( int c ) { REC ( i , D , c ) { L[R[i]] = L[i] ; R[L[i]] = R[i] ; } } void resume ( int c ) { REC ( i , U , c ) { L[R[i]] = i ; R[L[i]] = i ; } } int dance ( int d ) { if ( R[0] == 0 ) return 1 ; int c = R[0] ; REC ( i , R , 0 ) if ( S[c] > S[i] ) c = i ; REC ( i , D , c ) { if ( vis[row[i] ^ 1] ) continue ; remove ( i ) ; vis[row[i]] = 1 ; REC ( j , R , i ) remove ( j ) ; if ( dance ( d + 1 ) ) return 1 ; REC ( j , L , i ) resume ( j ) ; resume ( i ) ; vis[row[i]] = 0 ; } return 0 ; } void solve () { int k , x ; char s[10] ; nv = n ; init () ; CLR ( vis , 0 ) ; FOR ( i , 1 , n ) { scanf ( "%d" , &k ) ; while ( k -- ) { scanf ( "%d%s" , &x , s ) ; if ( s[1] == 'N' ) link ( x << 1 , i ) ; else link ( x << 1 | 1 , i ) ; } } if ( dance ( 0 ) ) { FOR ( i , 1 , m ) printf ( "%s%c" , vis[i << 1] ? "ON" : "OFF" , i < m ? ' ' : '\n' ) ; } else printf ( "-1\n" ) ; } } dlx ; int main () { while ( ~scanf ( "%d%d" , &dlx.n , &dlx.m ) ) dlx.solve () ; return 0 ; }