#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef LOCAL #include "debug.h" #else #define debug(...) 2137 #endif const uint32_t MOD = 1000000007; struct mint { uint32_t val; mint(ll v = 0) { v %= MOD; if (v < 0) { v += MOD; } val = v; } mint& operator+=(const mint& other) { val += other.val; if (val >= MOD) { val -= MOD; } return *this; } mint& operator-=(const mint& other) { val += MOD - other.val; if (val >= MOD) { val -= MOD; } return *this; } mint& operator*=(const mint& other) { val = (uint64_t)val * other.val % MOD; return *this; } mint& operator/=(const mint& other) { return *this *= other.inv(); } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } mint pow(ll exp) const { mint a = *this; mint res = 1; while (exp > 0) { if (exp & 1) { res *= a; } a *= a; exp /= 2; } return res; } mint inv() const { assert(val != 0); return pow(MOD - 2); } friend ostream& operator<<(ostream& os, const mint& m) { return os << m.val; } }; vector<mint> _f = {1}, _fi = {1}; void _init(int n) { int k = ssize(_f); if (n < k) { return; } n *= 2; _f.resize(n + 1); _fi.resize(n + 1); for (int i = k; i <= n; i++) { _f[i] = _f[i - 1] * i; } _fi[n] = _f[n].inv(); for (int i = n; i > k; i--) { _fi[i - 1] = _fi[i] * i; } } mint fact(int n) { _init(n); return _f[n]; } mint inv_fact(int n) { _init(n); return _fi[n]; } mint choose(int n, int k) { if (n < k || k < 0) { return 0; } _init(n); return _f[n] * _fi[k] * _fi[n - k]; } const int N = 200200; int n, m; int a[N]; vector<int> adj[N]; int c[N]; int sz = 0; bool czy = true; int cnt[2] = {0, 0}; int s[2] = {0, 0}; void dfs(int u) { cnt[c[u]] += a[u]; s[c[u]]++; sz++; for (int v : adj[u]) { if (c[v] == -1) { c[v] = c[u] ^ 1; dfs(v); } else if (c[v] != (c[u] ^ 1)) { czy = false; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } mint ans = 1; for (int i = 0; i < n; i++) { c[i] = -1; } for (int i = 0; i < n; i++) { if (c[i] != -1) { continue; } sz = 0; c[i] = 0; czy = true; cnt[0] = cnt[1] = s[0] = s[1] = 0; dfs(i); if (czy) { mint sum = 0; for (int j = 0; j <= s[0]; j++) { sum += choose(s[0], j) * choose(s[1], j - cnt[0] + cnt[1]); } ans *= sum; } else { ans *= mint(2).pow(sz - 1); } } cout << ans << '\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 170 171 172 173 174 175 176 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef LOCAL #include "debug.h" #else #define debug(...) 2137 #endif const uint32_t MOD = 1000000007; struct mint { uint32_t val; mint(ll v = 0) { v %= MOD; if (v < 0) { v += MOD; } val = v; } mint& operator+=(const mint& other) { val += other.val; if (val >= MOD) { val -= MOD; } return *this; } mint& operator-=(const mint& other) { val += MOD - other.val; if (val >= MOD) { val -= MOD; } return *this; } mint& operator*=(const mint& other) { val = (uint64_t)val * other.val % MOD; return *this; } mint& operator/=(const mint& other) { return *this *= other.inv(); } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } mint pow(ll exp) const { mint a = *this; mint res = 1; while (exp > 0) { if (exp & 1) { res *= a; } a *= a; exp /= 2; } return res; } mint inv() const { assert(val != 0); return pow(MOD - 2); } friend ostream& operator<<(ostream& os, const mint& m) { return os << m.val; } }; vector<mint> _f = {1}, _fi = {1}; void _init(int n) { int k = ssize(_f); if (n < k) { return; } n *= 2; _f.resize(n + 1); _fi.resize(n + 1); for (int i = k; i <= n; i++) { _f[i] = _f[i - 1] * i; } _fi[n] = _f[n].inv(); for (int i = n; i > k; i--) { _fi[i - 1] = _fi[i] * i; } } mint fact(int n) { _init(n); return _f[n]; } mint inv_fact(int n) { _init(n); return _fi[n]; } mint choose(int n, int k) { if (n < k || k < 0) { return 0; } _init(n); return _f[n] * _fi[k] * _fi[n - k]; } const int N = 200200; int n, m; int a[N]; vector<int> adj[N]; int c[N]; int sz = 0; bool czy = true; int cnt[2] = {0, 0}; int s[2] = {0, 0}; void dfs(int u) { cnt[c[u]] += a[u]; s[c[u]]++; sz++; for (int v : adj[u]) { if (c[v] == -1) { c[v] = c[u] ^ 1; dfs(v); } else if (c[v] != (c[u] ^ 1)) { czy = false; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } mint ans = 1; for (int i = 0; i < n; i++) { c[i] = -1; } for (int i = 0; i < n; i++) { if (c[i] != -1) { continue; } sz = 0; c[i] = 0; czy = true; cnt[0] = cnt[1] = s[0] = s[1] = 0; dfs(i); if (czy) { mint sum = 0; for (int j = 0; j <= s[0]; j++) { sum += choose(s[0], j) * choose(s[1], j - cnt[0] + cnt[1]); } ans *= sum; } else { ans *= mint(2).pow(sz - 1); } } cout << ans << '\n'; } |