#include<bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const ll MOD=1e9+7; const int LIM=2e5+7; vector<int>V[LIM]; ll T[LIM], kol[LIM], sil[LIM], osil[LIM], ile[3], sum[3]; bool dwu; ll pot(ll a, ll b) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%MOD; a=(a*a)%MOD; b/=2; } return ans; } ll C(ll n, ll k) { if(k>n) return 0; return (((sil[n]*osil[k])%MOD)*osil[n-k])%MOD; } void DFS(int x) { ++ile[kol[x]]; if(T[x]) ++sum[kol[x]]; for(auto i : V[x]) { if(!kol[i]) { kol[i]=kol[x]^3; DFS(i); } else if(kol[i]==kol[x]) dwu=false; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); sil[0]=osil[0]=1; for(ll i=1; i<LIM; ++i) { sil[i]=(sil[i-1]*i)%MOD; osil[i]=pot(sil[i], MOD-2); } int n, m; cin >> n >> m; rep(i, n) cin >> T[i]; rep(i, m) { int a, b; cin >> a >> b; --a; --b; V[a].pb(b); V[b].pb(a); } ll ans=1; rep(i, n) if(!kol[i]) { ile[1]=ile[2]=sum[1]=sum[2]=0; dwu=true; kol[i]=1; DFS(i); if(!dwu) { ans=(ans*pot(2, ile[1]+ile[2]-1))%MOD; continue; } if(dwu) ans=(ans*C(ile[1]+ile[2], sum[1]-sum[2]+ile[2]))%MOD; } cout << ans << '\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 | #include<bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const ll MOD=1e9+7; const int LIM=2e5+7; vector<int>V[LIM]; ll T[LIM], kol[LIM], sil[LIM], osil[LIM], ile[3], sum[3]; bool dwu; ll pot(ll a, ll b) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%MOD; a=(a*a)%MOD; b/=2; } return ans; } ll C(ll n, ll k) { if(k>n) return 0; return (((sil[n]*osil[k])%MOD)*osil[n-k])%MOD; } void DFS(int x) { ++ile[kol[x]]; if(T[x]) ++sum[kol[x]]; for(auto i : V[x]) { if(!kol[i]) { kol[i]=kol[x]^3; DFS(i); } else if(kol[i]==kol[x]) dwu=false; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); sil[0]=osil[0]=1; for(ll i=1; i<LIM; ++i) { sil[i]=(sil[i-1]*i)%MOD; osil[i]=pot(sil[i], MOD-2); } int n, m; cin >> n >> m; rep(i, n) cin >> T[i]; rep(i, m) { int a, b; cin >> a >> b; --a; --b; V[a].pb(b); V[b].pb(a); } ll ans=1; rep(i, n) if(!kol[i]) { ile[1]=ile[2]=sum[1]=sum[2]=0; dwu=true; kol[i]=1; DFS(i); if(!dwu) { ans=(ans*pot(2, ile[1]+ile[2]-1))%MOD; continue; } if(dwu) ans=(ans*C(ile[1]+ile[2], sum[1]-sum[2]+ile[2]))%MOD; } cout << ans << '\n'; } |