#include <algorithm> #include <iostream> #include <unordered_set> #include <vector> #include <bitset> using namespace std; int main() { int n, m; cin >> n >> m; bitset<32> start; for (int i = 0; i < n; i++) { int a; cin >> a; if (a == 1) start[i].flip(); } vector<pair<int, int>> swaps(m); int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; swaps[i] = {b - 1, a - 1}; } sort(swaps.begin(), swaps.end(), greater<pair<int, int>>()); unordered_set<bitset<32>> possible; possible.insert(start); unordered_set<bitset<32>> possible_copy; int last_b = -1; while (possible != possible_copy) { possible_copy = possible; for (auto &[b, a] : swaps) { unordered_set<bitset<32>> possible2; for (auto num : possible) { if (num[a] == num[b]) { bitset<32> copy_num = num; copy_num[a].flip(); copy_num[b].flip(); possible2.insert(copy_num); } possible2.insert(num); } possible = possible2; } } cout << possible.size() << endl; // for (auto &num : possible) { // for (int i = 0; i < n; i++) { // cout << num[i]; // } // cout << endl; // } 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 | #include <algorithm> #include <iostream> #include <unordered_set> #include <vector> #include <bitset> using namespace std; int main() { int n, m; cin >> n >> m; bitset<32> start; for (int i = 0; i < n; i++) { int a; cin >> a; if (a == 1) start[i].flip(); } vector<pair<int, int>> swaps(m); int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; swaps[i] = {b - 1, a - 1}; } sort(swaps.begin(), swaps.end(), greater<pair<int, int>>()); unordered_set<bitset<32>> possible; possible.insert(start); unordered_set<bitset<32>> possible_copy; int last_b = -1; while (possible != possible_copy) { possible_copy = possible; for (auto &[b, a] : swaps) { unordered_set<bitset<32>> possible2; for (auto num : possible) { if (num[a] == num[b]) { bitset<32> copy_num = num; copy_num[a].flip(); copy_num[b].flip(); possible2.insert(copy_num); } possible2.insert(num); } possible = possible2; } } cout << possible.size() << endl; // for (auto &num : possible) { // for (int i = 0; i < n; i++) { // cout << num[i]; // } // cout << endl; // } return 0; } |