// Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; #pragma GCC target ("avx") #define rep(i, a, b) for(int i = (a); i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif const ll mod = 1e9 + 7; ll modpow(ll b, ll e) { ll ans = 1; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } const int limit = int(2e5) + 7; ll fact[limit], inv_fact[limit]; void preprocess() { fact[0] = 1; rep(i,1,limit) fact[i] = fact[i-1] * i % mod; inv_fact[limit-1] = modpow(fact[limit-1], mod-2); for(ll i=limit-2;i>=0;i--) inv_fact[i] = inv_fact[i+1] * (i+1) % mod; } ll choose(int n, int k) { return (fact[n] * inv_fact[k] % mod) * inv_fact[n-k] % mod; } int n, m; vector<bool> lit; vi color; vector<vi> g; vi stk; bool is_biparte(int v) { stk.push_back(v); bool biparte = true; for(auto e : g[v]) { if(color[e] == -1) { color[e] = !color[v]; biparte &= is_biparte(e); } if(color[e] == color[v]) biparte = false; } return biparte; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); preprocess(); cin>>n>>m; g.resize(n); lit.resize(n); rep(i,0,n) { char c; cin>>c; lit[i] = c == '1'; } rep(i,0,m) { int a, b; cin>>a>>b; a--; b--; g[a].push_back(b); g[b].push_back(a); } vector<ll> powers(n+1, 1); rep(i,0,n) powers[i+1] = powers[i] * 2LL % mod; color.assign(n, -1); ll res = 1; rep(i,0,n) { if(color[i] != -1) continue; stk.clear(); color[i] = 0; bool biparte = is_biparte(i); if(!biparte) { res = res * powers[sz(stk)-1] % mod; continue; } int diff = 0, a = 0, b = 0; for(auto v : stk) { diff += (color[v] ? -1 : 1) * lit[v]; if(color[v]) a++; else b++; } dbg(diff, a, b, stk); ll num = 0; rep(x,max(0,-diff),min(a,b-diff)+1) { num = (num + choose(a,x) * choose(b,x+diff)) % mod; } dbg(num); res = res * num % 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 | // Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; #pragma GCC target ("avx") #define rep(i, a, b) for(int i = (a); i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif const ll mod = 1e9 + 7; ll modpow(ll b, ll e) { ll ans = 1; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } const int limit = int(2e5) + 7; ll fact[limit], inv_fact[limit]; void preprocess() { fact[0] = 1; rep(i,1,limit) fact[i] = fact[i-1] * i % mod; inv_fact[limit-1] = modpow(fact[limit-1], mod-2); for(ll i=limit-2;i>=0;i--) inv_fact[i] = inv_fact[i+1] * (i+1) % mod; } ll choose(int n, int k) { return (fact[n] * inv_fact[k] % mod) * inv_fact[n-k] % mod; } int n, m; vector<bool> lit; vi color; vector<vi> g; vi stk; bool is_biparte(int v) { stk.push_back(v); bool biparte = true; for(auto e : g[v]) { if(color[e] == -1) { color[e] = !color[v]; biparte &= is_biparte(e); } if(color[e] == color[v]) biparte = false; } return biparte; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); preprocess(); cin>>n>>m; g.resize(n); lit.resize(n); rep(i,0,n) { char c; cin>>c; lit[i] = c == '1'; } rep(i,0,m) { int a, b; cin>>a>>b; a--; b--; g[a].push_back(b); g[b].push_back(a); } vector<ll> powers(n+1, 1); rep(i,0,n) powers[i+1] = powers[i] * 2LL % mod; color.assign(n, -1); ll res = 1; rep(i,0,n) { if(color[i] != -1) continue; stk.clear(); color[i] = 0; bool biparte = is_biparte(i); if(!biparte) { res = res * powers[sz(stk)-1] % mod; continue; } int diff = 0, a = 0, b = 0; for(auto v : stk) { diff += (color[v] ? -1 : 1) * lit[v]; if(color[v]) a++; else b++; } dbg(diff, a, b, stk); ll num = 0; rep(x,max(0,-diff),min(a,b-diff)+1) { num = (num + choose(a,x) * choose(b,x+diff)) % mod; } dbg(num); res = res * num % mod; } cout<<res<<'\n'; } |