#include <bits/stdc++.h> #define FOR(i, a, b) for(int i =(a); i < (b); ++i) #define re(i, n) FOR(i, 1, n) #define ford(i, a, b) for(int i = (a); i >= (b); --i) #define rep(i, n) for(int i = 0;i <(n); ++i) #define all(x) (x).begin(), (x).end() #define sz(x) (int) (x).size() using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<pll> vpll; typedef vector<pii> vpii; const ll inf = 1e18; const int N = 5005; const int mod = 1e9 + 7; const int BASE = 3; vll twopow; int n, m; vector<pii> edges; unordered_set<ll> vis; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; twopow.resize(n+1); twopow[0] = 1; vector<bool> fstate(n); rep(i, n) { twopow[i+1] = twopow[i] * BASE; } rep(i, n) { bool f; cin >> f; fstate[i] = f; } rep(i, m) { int a, b; cin >> a >> b; a--; b--; edges.push_back({a, b}); } if (n <= 30) { int mask = 0; rep(i, n) { mask |= (fstate[i] << i); } vector<int> states; vector<bool> vis(1 << n); states.push_back(mask); ll cnt = 0; vis[mask] = true; while (!states.empty()) { cnt++; int v = states.back(); states.pop_back(); rep(i, m) { int a = edges[i].first; int b = edges[i].second; if (!(((v >> a) ^ (v >> b)) & 1)) { int u = v ^ ((1 << a) | (1 << b)); if (!vis[u]) { vis[u] = true; states.push_back(u); } } } } cout << cnt % mod << '\n'; } else { vector<pair<vector<bool>, ll>> states; ll fstatehash = 0ll; rep(i, n) fstatehash += fstate[i] * twopow[i]; states.push_back({fstate, fstatehash}); ll cnt = 0; vis.insert(fstatehash); while (!states.empty()) { cnt++; auto vp = states.back(); states.pop_back(); auto v = vp.first; ll h = vp.second; rep(i, m) { int a = edges[i].first; int b = edges[i].second; if (v[a] == v[b]) { ll uh; if (v[a]) uh = h - twopow[a] - twopow[b]; else uh = h + twopow[a] + twopow[b]; if (vis.find(uh) == vis.end()) { vis.insert(uh); v[a] = !v[a]; v[b] = !v[b]; states.push_back({v, uh}); v[a] = !v[a]; v[b] = !v[b]; } } } } cout << cnt % mod << '\n'; } }
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | #include <bits/stdc++.h> #define FOR(i, a, b) for(int i =(a); i < (b); ++i) #define re(i, n) FOR(i, 1, n) #define ford(i, a, b) for(int i = (a); i >= (b); --i) #define rep(i, n) for(int i = 0;i <(n); ++i) #define all(x) (x).begin(), (x).end() #define sz(x) (int) (x).size() using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<pll> vpll; typedef vector<pii> vpii; const ll inf = 1e18; const int N = 5005; const int mod = 1e9 + 7; const int BASE = 3; vll twopow; int n, m; vector<pii> edges; unordered_set<ll> vis; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; twopow.resize(n+1); twopow[0] = 1; vector<bool> fstate(n); rep(i, n) { twopow[i+1] = twopow[i] * BASE; } rep(i, n) { bool f; cin >> f; fstate[i] = f; } rep(i, m) { int a, b; cin >> a >> b; a--; b--; edges.push_back({a, b}); } if (n <= 30) { int mask = 0; rep(i, n) { mask |= (fstate[i] << i); } vector<int> states; vector<bool> vis(1 << n); states.push_back(mask); ll cnt = 0; vis[mask] = true; while (!states.empty()) { cnt++; int v = states.back(); states.pop_back(); rep(i, m) { int a = edges[i].first; int b = edges[i].second; if (!(((v >> a) ^ (v >> b)) & 1)) { int u = v ^ ((1 << a) | (1 << b)); if (!vis[u]) { vis[u] = true; states.push_back(u); } } } } cout << cnt % mod << '\n'; } else { vector<pair<vector<bool>, ll>> states; ll fstatehash = 0ll; rep(i, n) fstatehash += fstate[i] * twopow[i]; states.push_back({fstate, fstatehash}); ll cnt = 0; vis.insert(fstatehash); while (!states.empty()) { cnt++; auto vp = states.back(); states.pop_back(); auto v = vp.first; ll h = vp.second; rep(i, m) { int a = edges[i].first; int b = edges[i].second; if (v[a] == v[b]) { ll uh; if (v[a]) uh = h - twopow[a] - twopow[b]; else uh = h + twopow[a] + twopow[b]; if (vis.find(uh) == vis.end()) { vis.insert(uh); v[a] = !v[a]; v[b] = !v[b]; states.push_back({v, uh}); v[a] = !v[a]; v[b] = !v[b]; } } } } cout << cnt % mod << '\n'; } } |