#include <bits/stdc++.h> using namespace std; const int MOD = 1'000'000'007; const int N = 200'004; int fact[N], revFact[N]; vector <int> firstModRev(int n) { vector <int> rev(n + 1, 0); rev[1] = 1; for (int i = 2; i <= n; i++) { rev[i] = (-(long long) (MOD / i) * rev[MOD % i]) % MOD; if (rev[i] < 0) { rev[i] += MOD; } } return rev; } void prepareFact() { fact[0] = revFact[0] = 1; auto rev = firstModRev(N - 1); for (int i = 1; i < N; i++) { fact[i] = ((long long) i * fact[i - 1]) % MOD; revFact[i] = ((long long) rev[i] * revFact[i - 1]) % MOD; } } int binom(int n, int m) { if (n < m || m < 0) return 0; int res = ((long long) fact[n] * revFact[n - m]) % MOD; res = ((long long) res * revFact[m]) % MOD; return res; } int solve(int n, vector <int> &c, vector <vector<int>> &g) { int ans = 1; vector <int> par(n, -1); for (int v = 0; v < n; v++) if (par[v] == -1) { queue <int> q; q.push(v); par[v] = 0; bool bipartite = true; vector <int> verts; while (!q.empty()) { int u = q.front(); q.pop(); verts.push_back(u); for (int x : g[u]) { if (par[x] == -1) { par[x] = 1 - par[u]; q.push(x); } else if (par[x] == par[u]) { bipartite = false; } } } if (bipartite) { vector <int> numOnes(2, 0), numAll(2, 0); for (int v : verts) { numOnes[par[v]] += c[v]; numAll[par[v]]++; } int diff = numOnes[1] - numOnes[0], total = 0; for (int i = 0; i <= numAll[0]; i++) { int j = i + diff; if (j >= 0 && j <= numAll[1]) { total = (total + (long long) binom(numAll[0], i) * binom(numAll[1], j)) % MOD; } } ans = ((long long) ans * total) % MOD; } else { int pw = 1; for (int i = 1; i < verts.size(); i++) { pw *= 2; if (pw >= MOD) { pw -= MOD; } } ans = ((long long) ans * pw) % MOD; } } return ans; } int main() { ios_base::sync_with_stdio(false); prepareFact(); int n, m; cin >> n >> m; vector <int> c(n); for (int &ci : c) { cin >> ci; } vector <vector<int>> g(n); while (m--) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } cout << solve(n, c, g); 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 | #include <bits/stdc++.h> using namespace std; const int MOD = 1'000'000'007; const int N = 200'004; int fact[N], revFact[N]; vector <int> firstModRev(int n) { vector <int> rev(n + 1, 0); rev[1] = 1; for (int i = 2; i <= n; i++) { rev[i] = (-(long long) (MOD / i) * rev[MOD % i]) % MOD; if (rev[i] < 0) { rev[i] += MOD; } } return rev; } void prepareFact() { fact[0] = revFact[0] = 1; auto rev = firstModRev(N - 1); for (int i = 1; i < N; i++) { fact[i] = ((long long) i * fact[i - 1]) % MOD; revFact[i] = ((long long) rev[i] * revFact[i - 1]) % MOD; } } int binom(int n, int m) { if (n < m || m < 0) return 0; int res = ((long long) fact[n] * revFact[n - m]) % MOD; res = ((long long) res * revFact[m]) % MOD; return res; } int solve(int n, vector <int> &c, vector <vector<int>> &g) { int ans = 1; vector <int> par(n, -1); for (int v = 0; v < n; v++) if (par[v] == -1) { queue <int> q; q.push(v); par[v] = 0; bool bipartite = true; vector <int> verts; while (!q.empty()) { int u = q.front(); q.pop(); verts.push_back(u); for (int x : g[u]) { if (par[x] == -1) { par[x] = 1 - par[u]; q.push(x); } else if (par[x] == par[u]) { bipartite = false; } } } if (bipartite) { vector <int> numOnes(2, 0), numAll(2, 0); for (int v : verts) { numOnes[par[v]] += c[v]; numAll[par[v]]++; } int diff = numOnes[1] - numOnes[0], total = 0; for (int i = 0; i <= numAll[0]; i++) { int j = i + diff; if (j >= 0 && j <= numAll[1]) { total = (total + (long long) binom(numAll[0], i) * binom(numAll[1], j)) % MOD; } } ans = ((long long) ans * total) % MOD; } else { int pw = 1; for (int i = 1; i < verts.size(); i++) { pw *= 2; if (pw >= MOD) { pw -= MOD; } } ans = ((long long) ans * pw) % MOD; } } return ans; } int main() { ios_base::sync_with_stdio(false); prepareFact(); int n, m; cin >> n >> m; vector <int> c(n); for (int &ci : c) { cin >> ci; } vector <vector<int>> g(n); while (m--) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } cout << solve(n, c, g); return 0; } |