Switches(暴力)

题目描述
We have N switches with “on” and “off” state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to ki switches: Switch si1, si2, …, and siki. It is lighted when the number of switches that are “on” among these switches is congruent to pi modulo 2.

How many combinations of “on” and “off” states of the switches light all the bulbs?

Constraints
·1≤N,M≤10
·1≤ki≤N
·1≤sij≤N
·sia≠sib(a≠b)
·pi is 0 or 1.
·All values in input are integers.

输入
Input is given from Standard Input in the following format:

N M
k1 s11 s12 … s1k1
:
kM sM1 sM2 … sMkM
p1 p2 … pM

输出
Print the number of combinations of “on” and “off” states of the switches that light all the bulbs.

样例输入
【样例1】
2 2
2 1 2
1 2
0 1
【样例2】
2 3
2 1 2
1 1
1 2
0 0 1
【样例3】
5 2
3 1 2 5
2 2 3
1 0

样例输出
【样例1】
1
【样例2】
0
【样例3】
8

提示
样例1解释
·Bulb 1 is lighted when there is an even number of switches that are “on” among the following: ·Switch 1 and 2.Bulb 2 is lighted when there is an odd number of switches that are “on” among the following: Switch 2.
There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

样例2解释
·Bulb 1 is lighted when there is an even number of switches that are “on” among the following: Switch 1 and 2.
·Bulb 2 is lighted when there is an even number of switches that are “on” among the following: Switch 1.
·Bulb 3 is lighted when there is an odd number of switches that are “on” among the following: Switch 2.
Switch 1 has to be “off” to light Bulb 2 and Switch 2 has to be “on” to light Bulb 3, but then Bulb
1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

思路
因为1≤N,M≤10,所以直接暴力即可求解

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=55;
const int M=4005;
const int INF=0x3f3f3f;
const ull sed=31;
const int mod=1e8+7;
const double eps=1e-8;
const double PI=acos(-1.0);
typedef pair<int,int>P;
 
int n,m,ans;
vector<int>sw[N];
int p[N],temp[N];
bool vis[N];
 
void dfs(int x)
{
    if(x==n+1)
    {
        memset(temp,0,sizeof(temp));
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]) continue;
            for(int j=0;j<sw[i].size();j++)
            {
                int b=sw[i][j];
                temp[b]++;
            }
        }
        for(int i=1;i<=m;i++) if(temp[i]%2!=p[i]) return ;
        ans++;
        return ;
    }
    dfs(x+1);
    vis[x]=true;
    dfs(x+1);
    vis[x]=false;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int k;
        scanf("%d",&k);
        for(int j=0;j<k;j++)
        {
            int t;
            scanf("%d",&t);
            sw[t].push_back(i);
        }
    }
    for(int i=1;i<=m;i++) scanf("%d",&p[i]);
    dfs(1);
    printf("%d\n",ans);
    return 0;
}
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值