#include <bits/stdc++.h> using namespace std; #define ll long long #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,b) rng(i,0,b) typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef pair<int,int> ii; template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; const int MOD = 1e9+7; ll read(){ ll i; cin>>i; return i; } vi readvi(int n,int off=0,int shift=0){ vi v(n+shift); rep(i,shift)v[i]=0; rep(i,n)v[i+shift]=read()+off; return v; } void YesNo(bool condition, bool do_exit=true) { if (condition) cout << "Yes" << endl; else cout << "No" << endl; if (do_exit) exit(0); } //a^b mod m, log(b) operations. //assumes a*m fits long long. long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } //a^b mod m for prime m (by Fermat's little thm ll prime_binpow(ll a, ll b, ll m) { return binpow (a, b%(m-1), m); } //modular inverse of a, using Fermat's little thm ll inverse(ll a, ll m) { return binpow (a, m-2, m); } vl get_factorials_modulo(int n, int m = MOD) { vl res; res.push_back(1); rng(i,1,n+1) res.push_back((res.back()*i) % m); return res; } ll binom_modulo(int n, int k, vl & fac, int m = MOD) { ll res = fac[n]; res = (res * inverse(fac[k],m)) % MOD; res = (res * inverse(fac[n-k],m)) % MOD; return res; } int main(void ) { ios::sync_with_stdio(false); cin.tie(NULL); int n,m; cin >> n >> m; vl fac = get_factorials_modulo(n); vi bits = readvi(n); vvi neighbors(n); rep(i,m) { int a,b; cin >> a >> b; --a; --b; neighbors[a].push_back(b); neighbors[b].push_back(a); } vector<char> color(n, -1); ll res = 1; rep(i,n) if (color[i] == -1) { color[i] = 0; bool bipartite = true; stack<int> s; s.push(i); int size[2] = {1, 0}; int ones[2] = {bits[i], 0}; while (not s.empty()) { int v = s.top(); s.pop(); for (int w : neighbors[v]) if (color[w] == color[v]) bipartite = false; else if (color[w] == -1) { color[w] = 1-color[v]; s.push(w); ++size[color[w]]; ones[color[w]] += bits[w]; } } ll local_res = 0; if (not bipartite) { int num_ones = ones[0] + ones[1]; int comp_size = size[0]+size[1]; for (int k = num_ones % 2; k <= comp_size; k += 2) local_res = (local_res + binom_modulo(size[0] + size[1], k, fac)) % MOD; } else { // bipartite int a; if (ones[0] >= ones[1]) a = 0; else a = 1; int b = 1-a; int t = ones[a] - ones[b]; rep(k,min(size[a]-t,size[b])+1) local_res = (local_res + binom_modulo(size[a],t+k,fac) * binom_modulo(size[b],k,fac)) % MOD; } res = (res * local_res) % MOD; } cout << res << endl; 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 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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,b) rng(i,0,b) typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef pair<int,int> ii; template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; const int MOD = 1e9+7; ll read(){ ll i; cin>>i; return i; } vi readvi(int n,int off=0,int shift=0){ vi v(n+shift); rep(i,shift)v[i]=0; rep(i,n)v[i+shift]=read()+off; return v; } void YesNo(bool condition, bool do_exit=true) { if (condition) cout << "Yes" << endl; else cout << "No" << endl; if (do_exit) exit(0); } //a^b mod m, log(b) operations. //assumes a*m fits long long. long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } //a^b mod m for prime m (by Fermat's little thm ll prime_binpow(ll a, ll b, ll m) { return binpow (a, b%(m-1), m); } //modular inverse of a, using Fermat's little thm ll inverse(ll a, ll m) { return binpow (a, m-2, m); } vl get_factorials_modulo(int n, int m = MOD) { vl res; res.push_back(1); rng(i,1,n+1) res.push_back((res.back()*i) % m); return res; } ll binom_modulo(int n, int k, vl & fac, int m = MOD) { ll res = fac[n]; res = (res * inverse(fac[k],m)) % MOD; res = (res * inverse(fac[n-k],m)) % MOD; return res; } int main(void ) { ios::sync_with_stdio(false); cin.tie(NULL); int n,m; cin >> n >> m; vl fac = get_factorials_modulo(n); vi bits = readvi(n); vvi neighbors(n); rep(i,m) { int a,b; cin >> a >> b; --a; --b; neighbors[a].push_back(b); neighbors[b].push_back(a); } vector<char> color(n, -1); ll res = 1; rep(i,n) if (color[i] == -1) { color[i] = 0; bool bipartite = true; stack<int> s; s.push(i); int size[2] = {1, 0}; int ones[2] = {bits[i], 0}; while (not s.empty()) { int v = s.top(); s.pop(); for (int w : neighbors[v]) if (color[w] == color[v]) bipartite = false; else if (color[w] == -1) { color[w] = 1-color[v]; s.push(w); ++size[color[w]]; ones[color[w]] += bits[w]; } } ll local_res = 0; if (not bipartite) { int num_ones = ones[0] + ones[1]; int comp_size = size[0]+size[1]; for (int k = num_ones % 2; k <= comp_size; k += 2) local_res = (local_res + binom_modulo(size[0] + size[1], k, fac)) % MOD; } else { // bipartite int a; if (ones[0] >= ones[1]) a = 0; else a = 1; int b = 1-a; int t = ones[a] - ones[b]; rep(k,min(size[a]-t,size[b])+1) local_res = (local_res + binom_modulo(size[a],t+k,fac) * binom_modulo(size[b],k,fac)) % MOD; } res = (res * local_res) % MOD; } cout << res << endl; return 0; } |