#include <bits/stdc++.h> #define ll long long #define debug if (0) const ll MOD = 1e9 + 7; const int MAX_N = 2e5; ll f[MAX_N + 3]; ll f_inv[MAX_N + 3]; int n, m; std::vector<int> list[MAX_N + 3]; int t[MAX_N + 3]; bool finished[MAX_N + 3]; int color[MAX_N + 3]; bool visited[MAX_N + 3]; ll fast_pow(ll a, int p) { if (p == 0) return 1; if (p % 2 == 0) { ll tmp = fast_pow(a, p / 2); return tmp * tmp % MOD; } return a * fast_pow(a, p - 1) % MOD; } void precalc_f() { f[0] = f_inv[0] = 1; for (ll i = 1; i <= n; i++) { f[i] = f[i - 1] * i % MOD; f_inv[i] = fast_pow(f[i], MOD - 2); } } ll choose(int n, int k) { if (k > n) return 0; return f[n] * f_inv[k] % MOD * f_inv[n - k] % MOD; } bool dfs_color(int v) { visited[v] = true; for (auto &u : list[v]) { if (visited[u] && color[u] == color[v]) return false; if (!visited[u]) { color[u] = (color[v] + 1) % 2; if (!dfs_color(u)) return false; } } return true; } void dfs_cnt(int v, std::vector<int> &ver) { ver.push_back(v); visited[v] = true; for (auto &u : list[v]) if (!visited[u]) dfs_cnt(u, ver); } ll solve_cmp(int v) { debug std::cout << "!!! solve_cmp(" << v << ")\n"; std::vector<int> vertices; dfs_cnt(v, vertices); for (auto &u : vertices) visited[u] = false; color[v] = 0; ll R = 0; ll B, C; if (dfs_color(v)) { // is bipartite int N1, N2, K1, K2; N1 = N2 = K1 = K2 = 0; for (auto &u : vertices) { if (color[u] == 0) { N1++; if (t[u] == 1) K1++; } else { N2++; if (t[u] == 1) K2++; } } if (K1 > K2) { std::swap(K1, K2); std::swap(N1, N2); } int dk = K2 - K1; int k2; debug { std::cout << "It is bipartite:\n"; std::cout << "N1 = " << N1 << ", K1 = " << K1 << "\n"; std::cout << "N2 = " << N2 << ", K2 = " << K2 << "\n"; } for (int k1 = 0; k1 <= N1 && k1 + dk <= N2; k1++) { k2 = k1 + dk; B = choose(N1, k1); C = choose(N2, k2); debug std::cout << "Adding (" << N1 << " choose " << k1 << ") * (" << N2 << " choose " << k2 << ")\n"; B = B * C % MOD; R = (R + B) % MOD; } } else { // is not bipartite ll N = vertices.size(); ll K = 0; for (auto &u : vertices) if (t[u] == 1) K++; debug std::cout << "N = " << N << ", K = " << K << "\n"; // (N choose K) + (N choose K + 2) + ... // + (N choose K-2) + ... for (int k = K % 2; k <= N; k += 2) { debug std::cout << "R += (" << N << " choose " << k << ") = " << choose(N, k) << "\n"; R = (R + choose(N, k)) % MOD; } } for (auto &u : vertices) finished[u] = true; debug std::cout << "Returning " << R << "\n"; return R; } void input() { std::cin >> n >> m; for (int i = 1; i <= n; i++) std::cin >> t[i]; for (int i = 1; i <= m; i++) { int x, y; std::cin >> x >> y; list[x].push_back(y); list[y].push_back(x); } } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); input(); precalc_f(); ll R = 1; for (int v = 1; v <= n; v++) if (!finished[v]) R = R * solve_cmp(v) % MOD; std::cout << R << "\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 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 166 167 168 169 | #include <bits/stdc++.h> #define ll long long #define debug if (0) const ll MOD = 1e9 + 7; const int MAX_N = 2e5; ll f[MAX_N + 3]; ll f_inv[MAX_N + 3]; int n, m; std::vector<int> list[MAX_N + 3]; int t[MAX_N + 3]; bool finished[MAX_N + 3]; int color[MAX_N + 3]; bool visited[MAX_N + 3]; ll fast_pow(ll a, int p) { if (p == 0) return 1; if (p % 2 == 0) { ll tmp = fast_pow(a, p / 2); return tmp * tmp % MOD; } return a * fast_pow(a, p - 1) % MOD; } void precalc_f() { f[0] = f_inv[0] = 1; for (ll i = 1; i <= n; i++) { f[i] = f[i - 1] * i % MOD; f_inv[i] = fast_pow(f[i], MOD - 2); } } ll choose(int n, int k) { if (k > n) return 0; return f[n] * f_inv[k] % MOD * f_inv[n - k] % MOD; } bool dfs_color(int v) { visited[v] = true; for (auto &u : list[v]) { if (visited[u] && color[u] == color[v]) return false; if (!visited[u]) { color[u] = (color[v] + 1) % 2; if (!dfs_color(u)) return false; } } return true; } void dfs_cnt(int v, std::vector<int> &ver) { ver.push_back(v); visited[v] = true; for (auto &u : list[v]) if (!visited[u]) dfs_cnt(u, ver); } ll solve_cmp(int v) { debug std::cout << "!!! solve_cmp(" << v << ")\n"; std::vector<int> vertices; dfs_cnt(v, vertices); for (auto &u : vertices) visited[u] = false; color[v] = 0; ll R = 0; ll B, C; if (dfs_color(v)) { // is bipartite int N1, N2, K1, K2; N1 = N2 = K1 = K2 = 0; for (auto &u : vertices) { if (color[u] == 0) { N1++; if (t[u] == 1) K1++; } else { N2++; if (t[u] == 1) K2++; } } if (K1 > K2) { std::swap(K1, K2); std::swap(N1, N2); } int dk = K2 - K1; int k2; debug { std::cout << "It is bipartite:\n"; std::cout << "N1 = " << N1 << ", K1 = " << K1 << "\n"; std::cout << "N2 = " << N2 << ", K2 = " << K2 << "\n"; } for (int k1 = 0; k1 <= N1 && k1 + dk <= N2; k1++) { k2 = k1 + dk; B = choose(N1, k1); C = choose(N2, k2); debug std::cout << "Adding (" << N1 << " choose " << k1 << ") * (" << N2 << " choose " << k2 << ")\n"; B = B * C % MOD; R = (R + B) % MOD; } } else { // is not bipartite ll N = vertices.size(); ll K = 0; for (auto &u : vertices) if (t[u] == 1) K++; debug std::cout << "N = " << N << ", K = " << K << "\n"; // (N choose K) + (N choose K + 2) + ... // + (N choose K-2) + ... for (int k = K % 2; k <= N; k += 2) { debug std::cout << "R += (" << N << " choose " << k << ") = " << choose(N, k) << "\n"; R = (R + choose(N, k)) % MOD; } } for (auto &u : vertices) finished[u] = true; debug std::cout << "Returning " << R << "\n"; return R; } void input() { std::cin >> n >> m; for (int i = 1; i <= n; i++) std::cin >> t[i]; for (int i = 1; i <= m; i++) { int x, y; std::cin >> x >> y; list[x].push_back(y); list[y].push_back(x); } } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); input(); precalc_f(); ll R = 1; for (int v = 1; v <= n; v++) if (!finished[v]) R = R * solve_cmp(v) % MOD; std::cout << R << "\n"; } |