1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>
#include <unordered_set>
#include <array>
#include <queue>
#include <bitset>

using namespace std;

#define BITSET_SIZE 100

void input(int & n, int & m, bitset<BITSET_SIZE> & bulbs, vector<array<int,2>> & switches) {
    cin >> n >> m;
    for(int i = 0; i < n; i++) {
        int a;
        cin >> a;
        bulbs[i] = (bool)a;
    }
    switches.resize(m);
    for(int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        switches[i] = {a, b};
    }
}

int get_result(bitset<BITSET_SIZE> const & bulbs, vector<array<int,2>> const & switches) {
    unordered_set<bitset<BITSET_SIZE>> reached_states;
    queue<bitset<BITSET_SIZE>> to_process;

    to_process.push(bulbs);
    reached_states.insert(bulbs);

    while(!to_process.empty()) {
        for(auto const & s : switches) {
            if(to_process.front()[s[0]] == to_process.front()[s[1]]) {
                to_process.front().flip(s[0]);
                to_process.front().flip(s[1]);
                if(reached_states.count(to_process.front()) == 0) {
                    to_process.push(to_process.front());
                    reached_states.insert(to_process.front());
                }
                to_process.front().flip(s[0]);
                to_process.front().flip(s[1]);
            }
        }
        to_process.pop();
    }

    return reached_states.size();
}

void solve() {
    int n, m;
    bitset<BITSET_SIZE> bulbs;
    vector<array<int,2>> switches;
    input(n, m, bulbs, switches); 
    
    cout << get_result(bulbs, switches) << "\n";
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    solve();

    return 0;
}