/* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif const LL mod = 1e9 + 7; struct Binom { int s; vector<LL> fact, inv, inv_fact, pow2; Binom(int _s) { s = _s; fact.resize(s + 1, 1); inv.resize(s + 1, 1); inv_fact.resize(s + 1, 1); pow2.resize(s + 1, 1); pow2[1] = 2; FOR(i, 2, s) { pow2[i] = pow2[i - 1] * 2 % mod; fact[i] = fact[i - 1] * i % mod; inv[i] = (mod - inv[mod % i] * (mod / i) % mod) % mod; inv_fact[i] = inv_fact[i - 1] * inv[i] % mod; } } LL get(int n, int k) { if (n < k || k < 0) { return 0; } return fact[n] * inv_fact[k] % mod * inv_fact[n - k] % mod; } LL get_pow(int ind) { return pow2[ind]; } }; struct Graph { int n, m; vector<int> bits; vector<vector<int> > kraw; vector<bool> vis; vector<int> col; array<int, 2> l; array<int, 2> k; bool bipart; Graph(int _n, int _m, vector<vector<int> > &_kraw, vector<int> &_bits) { n = _n; m = _m; swap(kraw, _kraw); swap(bits, _bits); vis.resize(n); col.resize(n); } void check_col(int x, int curr) { col[x] = curr; vis[x] = true; l[curr - 1]++; k[curr - 1] += bits[x]; for (int v : kraw[x]) { if (vis[v] && col[x] == col[v]) { bipart = false; } if (!vis[v]) { check_col(v, 3 - curr); } } } LL solve() { Binom binom(n); LL res = 1; REP(i, n) { if (kraw[i].empty()) { continue; } if (!vis[i]) { l = {0, 0}; k = {0, 0}; bipart = true; check_col(i, 1); if (!bipart) { res = res * binom.get_pow(l[0] + l[1] - 1) % mod; } else { int ile = min(k[0], k[1]); k[0] -= ile; k[1] -= ile; // debug(k, l); LL new_res = 0; REP(j, min(l[0], l[1])+1) { // debug(l[0], k[0]+j, l[1], k[1]+j); new_res = (new_res + binom.get(l[0], k[0]+j) * binom.get(l[1], k[1]+j)) % mod; } res = res * new_res % mod; } } } return res; } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector<int> bits(n); vector<vector<int> > kraw(n); REP(i, n) { cin >> bits[i]; } REP(i, m) { int a, b; cin >> a >> b; a--; b--; kraw[a].push_back(b); kraw[b].push_back(a); } Graph g(n, m, kraw, bits); cout << g.solve() << "\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 | /* * Opis: Główny nagłówek */ #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif const LL mod = 1e9 + 7; struct Binom { int s; vector<LL> fact, inv, inv_fact, pow2; Binom(int _s) { s = _s; fact.resize(s + 1, 1); inv.resize(s + 1, 1); inv_fact.resize(s + 1, 1); pow2.resize(s + 1, 1); pow2[1] = 2; FOR(i, 2, s) { pow2[i] = pow2[i - 1] * 2 % mod; fact[i] = fact[i - 1] * i % mod; inv[i] = (mod - inv[mod % i] * (mod / i) % mod) % mod; inv_fact[i] = inv_fact[i - 1] * inv[i] % mod; } } LL get(int n, int k) { if (n < k || k < 0) { return 0; } return fact[n] * inv_fact[k] % mod * inv_fact[n - k] % mod; } LL get_pow(int ind) { return pow2[ind]; } }; struct Graph { int n, m; vector<int> bits; vector<vector<int> > kraw; vector<bool> vis; vector<int> col; array<int, 2> l; array<int, 2> k; bool bipart; Graph(int _n, int _m, vector<vector<int> > &_kraw, vector<int> &_bits) { n = _n; m = _m; swap(kraw, _kraw); swap(bits, _bits); vis.resize(n); col.resize(n); } void check_col(int x, int curr) { col[x] = curr; vis[x] = true; l[curr - 1]++; k[curr - 1] += bits[x]; for (int v : kraw[x]) { if (vis[v] && col[x] == col[v]) { bipart = false; } if (!vis[v]) { check_col(v, 3 - curr); } } } LL solve() { Binom binom(n); LL res = 1; REP(i, n) { if (kraw[i].empty()) { continue; } if (!vis[i]) { l = {0, 0}; k = {0, 0}; bipart = true; check_col(i, 1); if (!bipart) { res = res * binom.get_pow(l[0] + l[1] - 1) % mod; } else { int ile = min(k[0], k[1]); k[0] -= ile; k[1] -= ile; // debug(k, l); LL new_res = 0; REP(j, min(l[0], l[1])+1) { // debug(l[0], k[0]+j, l[1], k[1]+j); new_res = (new_res + binom.get(l[0], k[0]+j) * binom.get(l[1], k[1]+j)) % mod; } res = res * new_res % mod; } } } return res; } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector<int> bits(n); vector<vector<int> > kraw(n); REP(i, n) { cin >> bits[i]; } REP(i, m) { int a, b; cin >> a >> b; a--; b--; kraw[a].push_back(b); kraw[b].push_back(a); } Graph g(n, m, kraw, bits); cout << g.solve() << "\n"; } |