#include <bits/stdc++.h> using namespace std; using ll = long long; const int MAXN = 2e5 + 7; const int MOD = 1e9 + 7; const int P = 313; int state[MAXN]; ll pot[MAXN]; vector<pair<int, int>> edges; unordered_set<ll> was; int n, m; ll akt; ll ans = 0; void backtrack(){ if(was.count(akt)){ return; } was.insert(akt); ans++; /*cout << akt << '\n'; for(int i = 0; i < n; i++){ cout << state[i] << ' '; } cout << '\n';*/ int u, v; ll prevHash; for(int i = 0; i < m; i++){ u = edges[i].first - 1; v = edges[i].second - 1; if(state[u] == state[v]){ prevHash = akt; if(state[u] == 1){ state[u] = 0; state[v] = 0; akt = (ll)akt - pot[u] - pot[v] + MOD; akt += MOD; akt %= MOD; }else{ state[u] = 1; state[v] = 1; akt = (ll)akt + pot[u] + pot[v] + MOD; akt += MOD; akt %= MOD; } backtrack(); akt = prevHash; if(state[u] == 1){ state[u] = 0; state[v] = 0; }else{ state[u] = 1; state[v] = 1; } } } } void preprocessing(){ pot[0] = 1; for(int i = 1; i < MAXN; i++){ pot[i] = (ll)pot[i - 1] * P; pot[i] %= MOD; } } ll getHash(string s){ ll hash = 0; for(int i = 0; i < n; i++){ hash = (ll)hash + (ll)pot[i] * s[i]; hash %= MOD; } return hash; } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; string now = ""; for(int i = 0; i < n; i++){ cin >> state[i]; if(state[i]){ now += '1'; }else{ now += '0'; } } preprocessing(); akt = getHash(now); //cout << akt << '\n'; int u, v; for(int i = 0; i < m; i++){ cin >> u >> v; edges.push_back({u, v}); } backtrack(); cout << ans << '\n'; return 0; }
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; const int MAXN = 2e5 + 7; const int MOD = 1e9 + 7; const int P = 313; int state[MAXN]; ll pot[MAXN]; vector<pair<int, int>> edges; unordered_set<ll> was; int n, m; ll akt; ll ans = 0; void backtrack(){ if(was.count(akt)){ return; } was.insert(akt); ans++; /*cout << akt << '\n'; for(int i = 0; i < n; i++){ cout << state[i] << ' '; } cout << '\n';*/ int u, v; ll prevHash; for(int i = 0; i < m; i++){ u = edges[i].first - 1; v = edges[i].second - 1; if(state[u] == state[v]){ prevHash = akt; if(state[u] == 1){ state[u] = 0; state[v] = 0; akt = (ll)akt - pot[u] - pot[v] + MOD; akt += MOD; akt %= MOD; }else{ state[u] = 1; state[v] = 1; akt = (ll)akt + pot[u] + pot[v] + MOD; akt += MOD; akt %= MOD; } backtrack(); akt = prevHash; if(state[u] == 1){ state[u] = 0; state[v] = 0; }else{ state[u] = 1; state[v] = 1; } } } } void preprocessing(){ pot[0] = 1; for(int i = 1; i < MAXN; i++){ pot[i] = (ll)pot[i - 1] * P; pot[i] %= MOD; } } ll getHash(string s){ ll hash = 0; for(int i = 0; i < n; i++){ hash = (ll)hash + (ll)pot[i] * s[i]; hash %= MOD; } return hash; } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; string now = ""; for(int i = 0; i < n; i++){ cin >> state[i]; if(state[i]){ now += '1'; }else{ now += '0'; } } preprocessing(); akt = getHash(now); //cout << akt << '\n'; int u, v; for(int i = 0; i < m; i++){ cin >> u >> v; edges.push_back({u, v}); } backtrack(); cout << ans << '\n'; return 0; } |