#include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> // macros #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d", &n); #define SC2(n, m) scanf("%d%d", &n, &m); #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; const int NMAX = 1<<20, MOD = 1000000007; int n, m, k; int cc_size, cc_ones, cc_red, cc_blue, zar_red, zar_blue, non_bip; int zar[NMAX], vis[NMAX]; vi edges[NMAX]; ll ans = 1; ll fact[NMAX]; ll power(ll x, int m) { //prd("power %lld %d\n", x, m); if (m==0) return 1; if (m==1) return x; if (m % 2) return (x * power(x, m-1)) % MOD; ll y = power(x, m/2); return (y * y) % MOD; } ll choose(int nn, int kk) { ll temp = (fact[nn] * power(fact[kk], MOD - 2)) % MOD; return (temp * power(fact[nn - kk], MOD - 2)) % MOD; } ll binom(int nn, int par) { prd("binom %d %d\n", nn, par); ll bin_ans = 0; FORE(i, 0, nn) if((i % 2) == par) { bin_ans = (bin_ans + choose(nn, i)) % MOD; prd("add %d = %lld\n", i, bin_ans); } return bin_ans; } void dfs(int v, int col) { vis[v] = col; if (col == 1) { cc_red++; zar_red += zar[v]; } else { cc_blue++; zar_blue += zar[v]; } int col2 = 3 - col; cc_size++; cc_ones += zar[v]; FORIT(it, edges[v], vi) { int u = *it; if (vis[u] == col2) continue; if (vis[u] == col) { non_bip = 1; continue; } dfs(u, col2); } } int main() { SC2(n, m); REP(i, n) SC(zar[i+1]); REP(i, m) { int u, v; SC2(u, v); edges[u].pb(v); edges[v].pb(u); } fact[0] = 1; FORE(i, 1, n) fact[i] = (fact[i-1] * i) % MOD; FORE(v, 1, n) if(!vis[v]) { cc_size = cc_ones = cc_red = cc_blue = zar_red = zar_blue = non_bip = 0; ll cc_ans = 0; dfs(v, 1); if (non_bip) cc_ans = binom(cc_size, cc_ones % 2); else { //int red_min_blue = zar_red - zar_blue; FORE(iblue, 0, cc_blue) { int ired = iblue + zar_red - zar_blue; if (ired < 0 || ired > cc_red) continue; cc_ans += choose(cc_red, ired) * choose(cc_blue, iblue); cc_ans = cc_ans % MOD; } } prd("cc_size %d cc_ones %d cc_red %d cc_blue %d zar_red %d zar_blie %d non_bip %d cc_ans %lld\n", cc_size, cc_ones, cc_red, cc_blue, zar_red, zar_blue, non_bip, cc_ans); ans = (ans * cc_ans) % MOD; } printf("%lld", 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 146 147 148 | #include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> // macros #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d", &n); #define SC2(n, m) scanf("%d%d", &n, &m); #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; const int NMAX = 1<<20, MOD = 1000000007; int n, m, k; int cc_size, cc_ones, cc_red, cc_blue, zar_red, zar_blue, non_bip; int zar[NMAX], vis[NMAX]; vi edges[NMAX]; ll ans = 1; ll fact[NMAX]; ll power(ll x, int m) { //prd("power %lld %d\n", x, m); if (m==0) return 1; if (m==1) return x; if (m % 2) return (x * power(x, m-1)) % MOD; ll y = power(x, m/2); return (y * y) % MOD; } ll choose(int nn, int kk) { ll temp = (fact[nn] * power(fact[kk], MOD - 2)) % MOD; return (temp * power(fact[nn - kk], MOD - 2)) % MOD; } ll binom(int nn, int par) { prd("binom %d %d\n", nn, par); ll bin_ans = 0; FORE(i, 0, nn) if((i % 2) == par) { bin_ans = (bin_ans + choose(nn, i)) % MOD; prd("add %d = %lld\n", i, bin_ans); } return bin_ans; } void dfs(int v, int col) { vis[v] = col; if (col == 1) { cc_red++; zar_red += zar[v]; } else { cc_blue++; zar_blue += zar[v]; } int col2 = 3 - col; cc_size++; cc_ones += zar[v]; FORIT(it, edges[v], vi) { int u = *it; if (vis[u] == col2) continue; if (vis[u] == col) { non_bip = 1; continue; } dfs(u, col2); } } int main() { SC2(n, m); REP(i, n) SC(zar[i+1]); REP(i, m) { int u, v; SC2(u, v); edges[u].pb(v); edges[v].pb(u); } fact[0] = 1; FORE(i, 1, n) fact[i] = (fact[i-1] * i) % MOD; FORE(v, 1, n) if(!vis[v]) { cc_size = cc_ones = cc_red = cc_blue = zar_red = zar_blue = non_bip = 0; ll cc_ans = 0; dfs(v, 1); if (non_bip) cc_ans = binom(cc_size, cc_ones % 2); else { //int red_min_blue = zar_red - zar_blue; FORE(iblue, 0, cc_blue) { int ired = iblue + zar_red - zar_blue; if (ired < 0 || ired > cc_red) continue; cc_ans += choose(cc_red, ired) * choose(cc_blue, iblue); cc_ans = cc_ans % MOD; } } prd("cc_size %d cc_ones %d cc_red %d cc_blue %d zar_red %d zar_blie %d non_bip %d cc_ans %lld\n", cc_size, cc_ones, cc_red, cc_blue, zar_red, zar_blue, non_bip, cc_ans); ans = (ans * cc_ans) % MOD; } printf("%lld", ans); return 0; } |