#include <iostream> #include <vector> #include <string> #include <numeric> #include <algorithm> #include <map> #include <set> #include <queue> #include <stack> #include <tuple> #include <unordered_set> struct VectorUCharHash { std::size_t operator()(const std::vector<unsigned char> &v) const { std::size_t hash = 0; for (unsigned char b : v) { hash = (hash << 8) | b; } return hash; } }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n, m; std::cin >> n >> m; std::vector<unsigned char> start(n, false); for (int i = 0; i < n; i++) { int c; std::cin >> c; start[i] = c; } std::vector<std::vector<int>> graph(n); for (int i = 0; i < m; i++) { int a, b; std::cin >> a >> b; graph[a - 1].push_back(b - 1); graph[b - 1].push_back(a - 1); } std::vector<unsigned char> checked(n, false); std::vector<unsigned char> checked2(n, false); std::vector<long long> results; for (int i = 0; i < n; i++) { if (checked[i]) { continue; } checked[i] = true; checked2[i] = true; std::unordered_set<std::vector<unsigned char>, VectorUCharHash> states; std::map<int, int> vertices; vertices[i] = 0; std::vector<unsigned char> state(1, start[i]); states.insert(state); std::queue<int> q; for (int j = 0; j < graph[i].size(); j++) { int next = graph[i][j]; q.push(next); checked[next] = true; } while (!q.empty()) { int current = q.front(); q.pop(); checked2[current] = true; vertices[current] = vertices.size(); std::queue<std::vector<unsigned char>> q2; std::unordered_set<std::vector<unsigned char>, VectorUCharHash> new_states; for (const auto &state : states) { std::vector<unsigned char> new_state = state; new_state.push_back(start[current]); new_states.insert(new_state); q2.push(new_state); } states.swap(new_states); while (!q2.empty()) { auto state = q2.front(); q2.pop(); for (const auto &vertexPair : vertices) { int vertex = vertexPair.first; for (int j = 0; j < graph[vertex].size(); j++) { int next = graph[vertex][j]; if (checked2[next]) { int i1 = vertices[vertex]; int i2 = vertices[next]; if (state[i1] == state[i2]) { std::vector<unsigned char> new_state = state; new_state[i1] = !state[i1]; new_state[i2] = !state[i2]; if (states.find(new_state) == states.end()) { states.insert(new_state); q2.push(new_state); q2.push(new_state); } } } } } } for (int j = 0; j < graph[current].size(); j++) { int next = graph[current][j]; if (!checked[next]) { q.push(next); checked[next] = true; } } } results.push_back(states.size()); } long long result = 1; const long long MOD = 1000000007; for (auto it = results.begin(); it != results.end(); it++) { result *= *it; result %= MOD; } std::cout << result << std::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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | #include <iostream> #include <vector> #include <string> #include <numeric> #include <algorithm> #include <map> #include <set> #include <queue> #include <stack> #include <tuple> #include <unordered_set> struct VectorUCharHash { std::size_t operator()(const std::vector<unsigned char> &v) const { std::size_t hash = 0; for (unsigned char b : v) { hash = (hash << 8) | b; } return hash; } }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n, m; std::cin >> n >> m; std::vector<unsigned char> start(n, false); for (int i = 0; i < n; i++) { int c; std::cin >> c; start[i] = c; } std::vector<std::vector<int>> graph(n); for (int i = 0; i < m; i++) { int a, b; std::cin >> a >> b; graph[a - 1].push_back(b - 1); graph[b - 1].push_back(a - 1); } std::vector<unsigned char> checked(n, false); std::vector<unsigned char> checked2(n, false); std::vector<long long> results; for (int i = 0; i < n; i++) { if (checked[i]) { continue; } checked[i] = true; checked2[i] = true; std::unordered_set<std::vector<unsigned char>, VectorUCharHash> states; std::map<int, int> vertices; vertices[i] = 0; std::vector<unsigned char> state(1, start[i]); states.insert(state); std::queue<int> q; for (int j = 0; j < graph[i].size(); j++) { int next = graph[i][j]; q.push(next); checked[next] = true; } while (!q.empty()) { int current = q.front(); q.pop(); checked2[current] = true; vertices[current] = vertices.size(); std::queue<std::vector<unsigned char>> q2; std::unordered_set<std::vector<unsigned char>, VectorUCharHash> new_states; for (const auto &state : states) { std::vector<unsigned char> new_state = state; new_state.push_back(start[current]); new_states.insert(new_state); q2.push(new_state); } states.swap(new_states); while (!q2.empty()) { auto state = q2.front(); q2.pop(); for (const auto &vertexPair : vertices) { int vertex = vertexPair.first; for (int j = 0; j < graph[vertex].size(); j++) { int next = graph[vertex][j]; if (checked2[next]) { int i1 = vertices[vertex]; int i2 = vertices[next]; if (state[i1] == state[i2]) { std::vector<unsigned char> new_state = state; new_state[i1] = !state[i1]; new_state[i2] = !state[i2]; if (states.find(new_state) == states.end()) { states.insert(new_state); q2.push(new_state); q2.push(new_state); } } } } } } for (int j = 0; j < graph[current].size(); j++) { int next = graph[current][j]; if (!checked[next]) { q.push(next); checked[next] = true; } } } results.push_back(states.size()); } long long result = 1; const long long MOD = 1000000007; for (auto it = results.begin(); it != results.end(); it++) { result *= *it; result %= MOD; } std::cout << result << std::endl; return 0; } |