#include<cstdio> #include<algorithm> #include<vector> #define S 200007 using namespace std; typedef long long ll; ll mod = 1000000007; vector<int> V[S]; int T[S]; int color[S]; bool vis[S]; vector<int> P; void DFS(int x){ vis[x] = 1; P.push_back(x); for(int i = 0; i < V[x].size(); i++){ int v = V[x][i]; if(!vis[v]){ color[v] = color[x] ^ 1; DFS(v); } } } ll pot(ll x, ll y){ if(y == 0) return 1; if(y == 1) return x; if(y % 2 == 0){ ll p = pot(x,y/2); return (p*p)%mod; }else{ return (pot(x,y-1) * x)%mod; } } ll sil[S]; ll f(int x, int y){ ll p = sil[x]; ll p2 = sil[y] * sil[x-y]; p2 %= mod; p2 = pot(p2, mod-2); p *= p2; p %= mod; return p; } int main(void){ sil[0] = 1; for(int i = 1; i < S; i++){ sil[i] = sil[i-1] * i; sil[i] %= mod; } int n,m; scanf("%d %d",&n,&m); for(int i = 1; i <= n; i++){ scanf("%d", &T[i]); } for(int i = 1; i <= m; i++){ int a,b; scanf("%d %d",&a,&b); V[a].push_back(b); V[b].push_back(a); } ll ans = 1; for(int i = 1; i <= n; i++){ if(!vis[i]){ P.clear(); DFS(i); bool cycle = false; bool good = false; int b1 = 0; int b2 = 0; int c1 = 0; int c2 = 0; for(int j = 0; j < P.size(); j++){ int v = P[j]; if(T[v]){ if(color[v]) b1++; else c1++; }else{ if(color[v]) b2++; else c2++; } for(int k = 0; k < V[v].size(); k++){ int u = V[v][k]; if(color[v] == color[u]) cycle = true; if(T[v] == T[u]) good = true; } } if(!good) continue; if(cycle){ ans *= pot(2,P.size()-1); ans %= mod; }else{ ll p = 0; for(int c = 0; c <= P.size(); c++){ int _b1 = b1+c1 - c; int _c1 = c; int _b2 = _b1 + (b2-b1); int _c2 = _c1 + (c2-c1); if(_b1 < 0 || _c1 < 0 || _b2 < 0 || _c2 < 0){ continue; } p += f(b1+c1, _c1) * f(b2+c2, _c2); p %= mod; } ans *= p; ans %= mod; } // printf("%d %lld*\n",i,ans); } } printf("%lld\n",ans); 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 | #include<cstdio> #include<algorithm> #include<vector> #define S 200007 using namespace std; typedef long long ll; ll mod = 1000000007; vector<int> V[S]; int T[S]; int color[S]; bool vis[S]; vector<int> P; void DFS(int x){ vis[x] = 1; P.push_back(x); for(int i = 0; i < V[x].size(); i++){ int v = V[x][i]; if(!vis[v]){ color[v] = color[x] ^ 1; DFS(v); } } } ll pot(ll x, ll y){ if(y == 0) return 1; if(y == 1) return x; if(y % 2 == 0){ ll p = pot(x,y/2); return (p*p)%mod; }else{ return (pot(x,y-1) * x)%mod; } } ll sil[S]; ll f(int x, int y){ ll p = sil[x]; ll p2 = sil[y] * sil[x-y]; p2 %= mod; p2 = pot(p2, mod-2); p *= p2; p %= mod; return p; } int main(void){ sil[0] = 1; for(int i = 1; i < S; i++){ sil[i] = sil[i-1] * i; sil[i] %= mod; } int n,m; scanf("%d %d",&n,&m); for(int i = 1; i <= n; i++){ scanf("%d", &T[i]); } for(int i = 1; i <= m; i++){ int a,b; scanf("%d %d",&a,&b); V[a].push_back(b); V[b].push_back(a); } ll ans = 1; for(int i = 1; i <= n; i++){ if(!vis[i]){ P.clear(); DFS(i); bool cycle = false; bool good = false; int b1 = 0; int b2 = 0; int c1 = 0; int c2 = 0; for(int j = 0; j < P.size(); j++){ int v = P[j]; if(T[v]){ if(color[v]) b1++; else c1++; }else{ if(color[v]) b2++; else c2++; } for(int k = 0; k < V[v].size(); k++){ int u = V[v][k]; if(color[v] == color[u]) cycle = true; if(T[v] == T[u]) good = true; } } if(!good) continue; if(cycle){ ans *= pot(2,P.size()-1); ans %= mod; }else{ ll p = 0; for(int c = 0; c <= P.size(); c++){ int _b1 = b1+c1 - c; int _c1 = c; int _b2 = _b1 + (b2-b1); int _c2 = _c1 + (c2-c1); if(_b1 < 0 || _c1 < 0 || _b2 < 0 || _c2 < 0){ continue; } p += f(b1+c1, _c1) * f(b2+c2, _c2); p %= mod; } ans *= p; ans %= mod; } // printf("%d %lld*\n",i,ans); } } printf("%lld\n",ans); return 0; } |