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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
 * Patryk Kisielewski
 * 
 * Potyczki Algorytmiczne 2024
 * Zadanie: ZAR - Żarówki [C]
*/
#include <unordered_set>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <utility>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <bitset>

using namespace std;


int n, m;
vector<bool> values;
vector<vector<int>> graph;
vector<bool> visited;

unordered_set<vector<bool>> combinations;

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;

    values = vector<bool>(n+1);
    graph = vector<vector<int>>(n+1);
    visited = vector<bool>(n+1);

    bool val;
    for (int i = 1; i <= n; ++i) {
        cin >> val;
        values[i] = val;
    }

    combinations.insert(values);

    int a, b;

    for (int i = 0; i < m; ++i) {
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    unsigned long long result = 1;
    size_t size = -1;

    while (size != combinations.size()) {
        size = combinations.size();
        visited =  vector<bool>(n+1, false);
        for (int i = 1; i <= n; ++i) {
            if (visited[i]) {
                continue;
            }
            queue<int> q;
            q.emplace(i);
            visited[i] = true;
            while (!q.empty()) {
                int v = q.front();
                q.pop();
                for (auto& u : graph[v]) {
                    auto cc = combinations;
                    for (auto c : cc) {
                        if (c[u] == c[v]) {
                            c[u] = !c[u];
                            c[v] = !c[v];
                            combinations.insert(c);
                        }
                    }
                    if (!visited[u]) {
                        q.emplace(u);
                        visited[u] = true;
                    }
                }
            }
        }
    }

    cout << combinations.size() << endl;

    return 0;
}