#include <iostream> #include <bitset> #include <unordered_set> constexpr int maxn = 2e5 + 7, maxm = 4e5 + 7, mod = 1e9 + 7; int n, m; std::unordered_set<std::bitset<maxn>> reachable; std::bitset<maxn> bs; std::pair<int, int> buttons[maxm]; void input() { scanf("%d%d", &n, &m); int x, y; for(int i = 0; i < n; i++) { scanf("%d", &x); bs[i] = x; } reachable.insert(bs); for(int i = 0; i < m; i++) { scanf("%d%d", &x, &y); buttons[i] = {x, y}; } } void brut() { reachable.insert(bs); for(int i = 0; i < m; i++) { auto [x, y] = buttons[i]; x--; y--; if(bs[x] != bs[y]) continue; bs[x] = !bs[x]; bs[y] = !bs[y]; if(reachable.find(bs) == reachable.end()) brut(); bs[x] = !bs[x]; bs[y] = !bs[y]; } } void debug() { for(const auto& b : reachable) { for(int i = 0; i < n; i++) std::cerr << b[i]; std::cerr << std::endl; } } int main() { input(); brut(); printf("%lld", reachable.size() % mod); // debug(); 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 | #include <iostream> #include <bitset> #include <unordered_set> constexpr int maxn = 2e5 + 7, maxm = 4e5 + 7, mod = 1e9 + 7; int n, m; std::unordered_set<std::bitset<maxn>> reachable; std::bitset<maxn> bs; std::pair<int, int> buttons[maxm]; void input() { scanf("%d%d", &n, &m); int x, y; for(int i = 0; i < n; i++) { scanf("%d", &x); bs[i] = x; } reachable.insert(bs); for(int i = 0; i < m; i++) { scanf("%d%d", &x, &y); buttons[i] = {x, y}; } } void brut() { reachable.insert(bs); for(int i = 0; i < m; i++) { auto [x, y] = buttons[i]; x--; y--; if(bs[x] != bs[y]) continue; bs[x] = !bs[x]; bs[y] = !bs[y]; if(reachable.find(bs) == reachable.end()) brut(); bs[x] = !bs[x]; bs[y] = !bs[y]; } } void debug() { for(const auto& b : reachable) { for(int i = 0; i < n; i++) std::cerr << b[i]; std::cerr << std::endl; } } int main() { input(); brut(); printf("%lld", reachable.size() % mod); // debug(); return 0; } |