一道水题
注意题中只要求扩展两层以内的开关
所以预处理出按了开关的影响,利用异或的性质就能很好地解决
然后$dfs$时枚举每个开关按或不按(因为即使再按也是相同的情况)
就没了$?$ 没了
代码如下
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 #define ll long long 6 using namespace std; 7 8 template <typename T> void in(T &x) { 9 x = 0; T f = 1; char ch = getchar(); 10 while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();} 11 while( isdigit(ch)) {x = 10 * x + ch - 48; ch = getchar();} 12 x *= f; 13 } 14 15 template <typename T> void out(T x) { 16 if(x < 0) x = -x , putchar('-'); 17 if(x > 9) out(x/10); 18 putchar(x%10 + 48); 19 } 20 //------------------------------------------------------- 21 22 const int N = 21; 23 24 int n,m,ans = 1e9+7; 25 int op[N],a[N][N]; 26 27 void init() { 28 int i,j,k; 29 for(i = 0;i < n; ++i) { 30 op[i] ^= (1<<i); 31 for(j = 0;j < n; ++j) { 32 if(a[i][j] && j != i) { 33 op[i] ^= (1<<j); 34 for(k = 0;k < n; ++k) { 35 if(a[j][k] && k != j) 36 op[i] ^= (1<<k); 37 } 38 } 39 } 40 } 41 } 42 43 void dfs(int k,int s,int cnt) { 44 if(cnt >= ans) return; 45 if(k == n) {if(!s) ans = cnt; return;} 46 dfs(k+1,s^op[k],cnt+1); 47 dfs(k+1,s,cnt); 48 } 49 50 int main() { 51 int i,j,k,x; 52 in(n); 53 for(i = 0;i < n; ++i) { 54 in(m); 55 for(j = 1;j <= m; ++j) { 56 in(x); --x; a[i][x] = 1; 57 } 58 } 59 init(); 60 dfs(0,(1<<n)-1,0); 61 if(ans == 1e9+7) cout << "Change an alarm clock,please!"; 62 else out(ans); 63 return 0; 64 }