#include <cstdio> #include <algorithm> #include <set> #include <queue> #include <iostream> using namespace std; int C[200001]; pair<int, int> z[400001]; int main() { int n, m; scanf("%d %d", &n, &m); unsigned long long d = 0; for (int i = 0; i < n; i++) { scanf("%d", C + i); d = d * 2 + C[i]; } for (int i = 0; i < m; i++) { scanf("%d %d", &z[i].first, &z[i].second); } int ret = 0; set<unsigned long long> S; S.insert(d); bool changed = true; while (changed) { int ile = S.size(); int cnt = 0; queue<unsigned long long> q; for (auto it : S) q.push(it); while (!q.empty()) { unsigned long long t = q.front(); q.pop(); for (int j = 0; j < m; j++) { unsigned long long a = n - z[j].first; unsigned long long b = n - z[j].second; if (((1LL << a) & t) > 0 && ((1LL << b) & t) > 0) { t -= 1LL<<a; t -= 1LL<<b; if (S.find(t) == S.end()) { q.push(t); S.insert(t); } } else if (((1LL << a) & t) == 0 && ((1LL << b) & t) == 0) { t += 1LL<<a; t += 1LL<<b; if (S.find(t) == S.end()) { q.push(t); S.insert(t); } } } } if (S.size() == ile) changed = false; } printf("%d\n",S.size()); 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 | #include <cstdio> #include <algorithm> #include <set> #include <queue> #include <iostream> using namespace std; int C[200001]; pair<int, int> z[400001]; int main() { int n, m; scanf("%d %d", &n, &m); unsigned long long d = 0; for (int i = 0; i < n; i++) { scanf("%d", C + i); d = d * 2 + C[i]; } for (int i = 0; i < m; i++) { scanf("%d %d", &z[i].first, &z[i].second); } int ret = 0; set<unsigned long long> S; S.insert(d); bool changed = true; while (changed) { int ile = S.size(); int cnt = 0; queue<unsigned long long> q; for (auto it : S) q.push(it); while (!q.empty()) { unsigned long long t = q.front(); q.pop(); for (int j = 0; j < m; j++) { unsigned long long a = n - z[j].first; unsigned long long b = n - z[j].second; if (((1LL << a) & t) > 0 && ((1LL << b) & t) > 0) { t -= 1LL<<a; t -= 1LL<<b; if (S.find(t) == S.end()) { q.push(t); S.insert(t); } } else if (((1LL << a) & t) == 0 && ((1LL << b) & t) == 0) { t += 1LL<<a; t += 1LL<<b; if (S.find(t) == S.end()) { q.push(t); S.insert(t); } } } } if (S.size() == ile) changed = false; } printf("%d\n",S.size()); return 0; } |