P4906 小奔关闹钟

${\color{Cyan}{>>Question}}$

一道水题

注意题中只要求扩展两层以内的开关

所以预处理出按了开关的影响,利用异或的性质就能很好地解决

然后$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 }

 

转载于:https://www.cnblogs.com/mzg1805/p/11356511.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值