#include <bits/stdc++.h> using namespace std; const int maxn = 1000, p = 1e9+321; int n, m, res; long long pot[maxn]; pair <int, int> con[maxn]; bool zap[maxn]; map <long long, bool> vis; long long idx() { long long w = 0; for (int i = 0; i < n; ++i) { w += zap[i] * pot[i]; } return w; } void back_track() { if (vis[idx()]) return; ++res; vis[idx()] = true; for (int j = 0; j < m; ++j) { pair <int, int> i = con[j]; if (zap[i.first] == zap[i.second]) { zap[i.second] = zap[i.first] = !zap[i.first]; back_track(); zap[i.second] = zap[i.first] = !zap[i.first]; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); pot[0] = 1; for (int i = 1; i < maxn; ++i) { pot[i] = (pot[i-1] * 2)%p; } cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> zap[i]; for (int i = 0; i < m; ++i) { cin >> con[i].first >> con[i].second; } back_track(); cout << res; }
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 | #include <bits/stdc++.h> using namespace std; const int maxn = 1000, p = 1e9+321; int n, m, res; long long pot[maxn]; pair <int, int> con[maxn]; bool zap[maxn]; map <long long, bool> vis; long long idx() { long long w = 0; for (int i = 0; i < n; ++i) { w += zap[i] * pot[i]; } return w; } void back_track() { if (vis[idx()]) return; ++res; vis[idx()] = true; for (int j = 0; j < m; ++j) { pair <int, int> i = con[j]; if (zap[i.first] == zap[i.second]) { zap[i.second] = zap[i.first] = !zap[i.first]; back_track(); zap[i.second] = zap[i.first] = !zap[i.first]; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); pot[0] = 1; for (int i = 1; i < maxn; ++i) { pot[i] = (pot[i-1] * 2)%p; } cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> zap[i]; for (int i = 0; i < m; ++i) { cin >> con[i].first >> con[i].second; } back_track(); cout << res; } |