#include <bits/stdc++.h> using namespace std; #define ll long long vector <bool> v; vector <bool> vis; vector <vector <int> > g; vector <int> gr; bool np = 0; const ll mod = 1e9+7; int sz = 0; array <int, 2> ilewg; array <int, 2> ile1wg; void dfs(int a) { vis[a] = 1; sz++; for (auto b : g[a]) { if (!vis[b]) { gr[b] = !gr[a]; ilewg[gr[b]]++; if (v[b]) ile1wg[gr[b]]++; dfs(b); } else if (gr[b] == gr[a]) { np = 1; } } } ll powmod(ll a, ll b) { ll res = 1; while (b > 0) { if (b % 2) res *= a; a *= a; a %= mod; res %= mod; b /= 2; } return res; } vector <ll> sil; ll dwumian(ll n, ll k) { ll res = sil[n]; res *= powmod(sil[k], mod-2); res %= mod; res *= powmod(sil[n-k], mod-2); res %= mod; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; g.resize(n); gr.resize(n); v.resize(n); vis.resize(n); for (int i = 0; i < n; i++) { bool x; cin >> x; v[i] = x; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } sil.resize(200000); sil[0] = 1; sil[1] = 1; for (int i = 2; i < 200000; i++) { sil[i] = sil[i-1] * i; sil[i] %= mod; } ll res = 1; for (int i = 0; i < n; i++) { if (!vis[i]) { np = 0; sz = 0; ilewg = {1, 0}; ile1wg = {0, 0}; if (v[i]) ile1wg[0]++; dfs(i); if (np) { ll temp = powmod(2, sz-1); res *= temp; res %= mod; } else { if (ile1wg[1] > ile1wg[0]) { swap(ile1wg[1], ile1wg[0]); swap(ilewg[1], ilewg[0]); } int roz = ile1wg[0] - ile1wg[1]; ll temp = 0; for (int j = 0; j <= min(ilewg[0] - roz, ilewg[1]); j++) { temp += dwumian(ilewg[0], j+roz) * dwumian(ilewg[1], j); temp %= mod; } res *= temp; res %= mod; } } } cout << res << "\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 | #include <bits/stdc++.h> using namespace std; #define ll long long vector <bool> v; vector <bool> vis; vector <vector <int> > g; vector <int> gr; bool np = 0; const ll mod = 1e9+7; int sz = 0; array <int, 2> ilewg; array <int, 2> ile1wg; void dfs(int a) { vis[a] = 1; sz++; for (auto b : g[a]) { if (!vis[b]) { gr[b] = !gr[a]; ilewg[gr[b]]++; if (v[b]) ile1wg[gr[b]]++; dfs(b); } else if (gr[b] == gr[a]) { np = 1; } } } ll powmod(ll a, ll b) { ll res = 1; while (b > 0) { if (b % 2) res *= a; a *= a; a %= mod; res %= mod; b /= 2; } return res; } vector <ll> sil; ll dwumian(ll n, ll k) { ll res = sil[n]; res *= powmod(sil[k], mod-2); res %= mod; res *= powmod(sil[n-k], mod-2); res %= mod; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; g.resize(n); gr.resize(n); v.resize(n); vis.resize(n); for (int i = 0; i < n; i++) { bool x; cin >> x; v[i] = x; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } sil.resize(200000); sil[0] = 1; sil[1] = 1; for (int i = 2; i < 200000; i++) { sil[i] = sil[i-1] * i; sil[i] %= mod; } ll res = 1; for (int i = 0; i < n; i++) { if (!vis[i]) { np = 0; sz = 0; ilewg = {1, 0}; ile1wg = {0, 0}; if (v[i]) ile1wg[0]++; dfs(i); if (np) { ll temp = powmod(2, sz-1); res *= temp; res %= mod; } else { if (ile1wg[1] > ile1wg[0]) { swap(ile1wg[1], ile1wg[0]); swap(ilewg[1], ilewg[0]); } int roz = ile1wg[0] - ile1wg[1]; ll temp = 0; for (int j = 0; j <= min(ilewg[0] - roz, ilewg[1]); j++) { temp += dwumian(ilewg[0], j+roz) * dwumian(ilewg[1], j); temp %= mod; } res *= temp; res %= mod; } } } cout << res << "\n"; } |