#include <cstdint> #include <iostream> #include <queue> #include <unordered_set> #include <vector> constexpr int XD = 1'000'000'007; struct config { config(const std::vector<bool> b): b(b) { for (int i = 0; i < b.size(); ++i) { if (b[i]) { hash ^= 1ULL << (i % (sizeof(size_t) * 8)); } } } void flip(int i) { b[i] = !b[i]; hash ^= 1ULL << (i % (sizeof(size_t) * 8)); } bool operator == (const config &other) const noexcept { for (int i = 0; i < b.size(); ++i) { if (b[i] != other.b[i]) { return false; } } return true; } std::vector<bool> b; size_t hash; }; template <> struct std::hash<config> { size_t operator()(const config &p) const noexcept { return p.hash; } }; int main() { int n, m; std::cin >> n >> m; std::vector<bool> init(n); for (auto b: init) { bool in; std::cin >> in; b = in; } std::vector<std::pair<int, int>> switches(m); for (auto &[l, r]: switches) { std::cin >> l >> r; } std::unordered_set<config> visited = { init }; std::queue<config> next; next.push(init); while (!next.empty()) { config ii = std::move(next.front()); next.pop(); for (auto [l, r]: switches) { if (ii.b[l - 1] == ii.b[r - 1]) { config iii = ii; iii.flip(l - 1); iii.flip(r - 1); if (!visited.count(iii)) { visited.insert(iii); next.push(iii); } } } } std::cout << visited.size() % XD << "\n"; }
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 <cstdint> #include <iostream> #include <queue> #include <unordered_set> #include <vector> constexpr int XD = 1'000'000'007; struct config { config(const std::vector<bool> b): b(b) { for (int i = 0; i < b.size(); ++i) { if (b[i]) { hash ^= 1ULL << (i % (sizeof(size_t) * 8)); } } } void flip(int i) { b[i] = !b[i]; hash ^= 1ULL << (i % (sizeof(size_t) * 8)); } bool operator == (const config &other) const noexcept { for (int i = 0; i < b.size(); ++i) { if (b[i] != other.b[i]) { return false; } } return true; } std::vector<bool> b; size_t hash; }; template <> struct std::hash<config> { size_t operator()(const config &p) const noexcept { return p.hash; } }; int main() { int n, m; std::cin >> n >> m; std::vector<bool> init(n); for (auto b: init) { bool in; std::cin >> in; b = in; } std::vector<std::pair<int, int>> switches(m); for (auto &[l, r]: switches) { std::cin >> l >> r; } std::unordered_set<config> visited = { init }; std::queue<config> next; next.push(init); while (!next.empty()) { config ii = std::move(next.front()); next.pop(); for (auto [l, r]: switches) { if (ii.b[l - 1] == ii.b[r - 1]) { config iii = ii; iii.flip(l - 1); iii.flip(r - 1); if (!visited.count(iii)) { visited.insert(iii); next.push(iii); } } } } std::cout << visited.size() % XD << "\n"; } |