#include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; typedef pair<long long,int> PLI; typedef pair<int,long long> PIL; const int mod = 1e9+7; vector<int> G[200005]; int col[200005]; int side[200005]; bool is_bip = true; vector<int> vertices; void dfs(int v){ vertices.push_back(v); for(int u: G[v]){ if(side[u] != -1){ if(side[u] == side[v]) is_bip = false; } else{ side[u] = 1-side[v]; dfs(u); } } } long long fact[200005]; long long rfact[200005]; long long pot(long long a, long long x){ if(x == 0) return 1; if(x&1) return pot(a, x-1)*a%mod; long long tmp = pot(a, x/2); return tmp*tmp%mod; } long long binom(int n, int k){ long long res = fact[n]*rfact[k]%mod; res = res*rfact[n-k]%mod; return res; } int main(){ ios_base::sync_with_stdio(0); int n,m; cin>>n>>m; for(int i=1;i<=n;i++) cin>>col[i]; for(int i=0;i<m;i++){ int a,b; cin>>a>>b; G[a].push_back(b); G[b].push_back(a); } for(int i=1;i<=n;i++) side[i] = -1; fact[0] = 1; for(int i=1;i<=n;i++) fact[i] = fact[i-1]*i%mod; for(int i=0;i<=n;i++) rfact[i] = pot(fact[i], mod-2); long long res = 1; for(int i=1;i<=n;i++){ if(side[i] != -1) continue; side[i] = 0; dfs(i); if(is_bip){ int cnt_black[2] = {0, 0}; int cnt[2] = {0, 0}; for(int v: vertices){ if(col[v] == 1){ cnt_black[side[v]]++; } cnt[side[v]]++; } if(cnt_black[0] < cnt_black[1]){ swap(cnt_black[0], cnt_black[1]); swap(cnt[0], cnt[1]); } long long tmp = 0; for(int k=cnt_black[0]-cnt_black[1];k<=cnt[0];k++){ int s = k-(cnt_black[0]-cnt_black[1]); if(s > cnt[1]) continue; // cout<<cnt[0]<<" "<<cnt[1]<<" "<<cnt_black[0]<<" "<<cnt_black[1]<<" "<<k<<" "<<s<<" "<<binom(cnt[0], k)<<" "<<binom(cnt[1], s)<<endl; tmp = (tmp+binom(cnt[0], k)*binom(cnt[1], s))%mod; } res = (res*tmp)%mod; } else{ // cout<<" "<<vertices.size()<<endl; for(int j=1;j<vertices.size();j++) res = (res*2)%mod; } is_bip = true; vertices.clear(); } cout<<res<<endl; }
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 | #include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; typedef pair<long long,int> PLI; typedef pair<int,long long> PIL; const int mod = 1e9+7; vector<int> G[200005]; int col[200005]; int side[200005]; bool is_bip = true; vector<int> vertices; void dfs(int v){ vertices.push_back(v); for(int u: G[v]){ if(side[u] != -1){ if(side[u] == side[v]) is_bip = false; } else{ side[u] = 1-side[v]; dfs(u); } } } long long fact[200005]; long long rfact[200005]; long long pot(long long a, long long x){ if(x == 0) return 1; if(x&1) return pot(a, x-1)*a%mod; long long tmp = pot(a, x/2); return tmp*tmp%mod; } long long binom(int n, int k){ long long res = fact[n]*rfact[k]%mod; res = res*rfact[n-k]%mod; return res; } int main(){ ios_base::sync_with_stdio(0); int n,m; cin>>n>>m; for(int i=1;i<=n;i++) cin>>col[i]; for(int i=0;i<m;i++){ int a,b; cin>>a>>b; G[a].push_back(b); G[b].push_back(a); } for(int i=1;i<=n;i++) side[i] = -1; fact[0] = 1; for(int i=1;i<=n;i++) fact[i] = fact[i-1]*i%mod; for(int i=0;i<=n;i++) rfact[i] = pot(fact[i], mod-2); long long res = 1; for(int i=1;i<=n;i++){ if(side[i] != -1) continue; side[i] = 0; dfs(i); if(is_bip){ int cnt_black[2] = {0, 0}; int cnt[2] = {0, 0}; for(int v: vertices){ if(col[v] == 1){ cnt_black[side[v]]++; } cnt[side[v]]++; } if(cnt_black[0] < cnt_black[1]){ swap(cnt_black[0], cnt_black[1]); swap(cnt[0], cnt[1]); } long long tmp = 0; for(int k=cnt_black[0]-cnt_black[1];k<=cnt[0];k++){ int s = k-(cnt_black[0]-cnt_black[1]); if(s > cnt[1]) continue; // cout<<cnt[0]<<" "<<cnt[1]<<" "<<cnt_black[0]<<" "<<cnt_black[1]<<" "<<k<<" "<<s<<" "<<binom(cnt[0], k)<<" "<<binom(cnt[1], s)<<endl; tmp = (tmp+binom(cnt[0], k)*binom(cnt[1], s))%mod; } res = (res*tmp)%mod; } else{ // cout<<" "<<vertices.size()<<endl; for(int j=1;j<vertices.size();j++) res = (res*2)%mod; } is_bip = true; vertices.clear(); } cout<<res<<endl; } |