#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator<<(auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator<<(auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << __LINE__ << ": [" #x "]: ", dump(x) #else #define debug(...) 0 #endif const ll MOD = 1e9 + 7; void DFS(int v, vector<vector<int>>& g, vector<int>& myColor, vector<int>& color, ll& cntA, ll& cntB, ll& diff, bool& twoColor){ if(myColor[v] == 1){ cntA++; }else{ cntB++; } if(color[v]){ if(myColor[v] == 1){ diff++; }else{ diff--; } } for(auto u : g[v]){ if(myColor[u] == 0){ myColor[u] = -myColor[v]; DFS(u, g, myColor, color, cntA, cntB, diff, twoColor); }else if(myColor[u] == myColor[v]){ twoColor = false; } } } long long power(long long a, long long b){ long long res = 1; while(b){ while(!(b&1)){ a = (a * a) % MOD; b>>=1; } res = (res * a) % MOD; b--; } return res; } vector<ll> inv; ll magic(ll a, ll b, ll d){ vector<ll> binomA(a+1); vector<ll> binomB(b+1); binomA[0] = 1; binomB[0] = 1; for(ll i= 1; i <= a; i++){ binomA[i] = (a-i + 1)*inv[i] % MOD; binomA[i] = (binomA[i] * binomA[i-1]) % MOD; } for(ll i= 1; i <= b; i++){ binomB[i] = (b-i + 1)*inv[i] % MOD; binomB[i] = (binomB[i] * binomB[i-1]) % MOD; } ll ans = 0; for(ll k = max(0ll, -d); k <= min(b, a- d); k++){ ans += binomA[k+d] * binomB[k]; ans %= MOD; } return ans; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m; cin>>n>>m; inv.resize(n+1); vector<ll> fact(n+1); fact[0] = 1; for(ll i = 1; i <= n; i++)fact[i] = (fact[i-1] * i) % MOD; ll x = power(fact[n], MOD - 2); for(ll i = n; i >= 1; i--){ inv[i] = (x * fact[i-1]) % MOD; x = (x * i) % MOD; } debug(inv); vector<int> color(n+1); rep(i, n)cin>>color[i+1]; vector<vector<int>> g(n+1); rep(i, m){ int a, b; cin>>a>>b; g[a].push_back(b); g[b].push_back(a); } ll res = 1; vector<int> myColor(n+1, 0); for(int v = 1; v <= n; v++){ if(myColor[v] == 0){ myColor[v] = 1; ll cntA = 0; ll cntB = 0; ll diff = 0; bool twoColor = true; DFS(v, g, myColor, color, cntA, cntB, diff, twoColor); debug(twoColor, cntA, cntB, diff); if(twoColor){ res = (res * magic(cntA, cntB, diff)) % MOD; }else{ res = (res * power(2, cntA + cntB - 1)) % MOD; } } } cout<<res<<"\n"; 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 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator<<(auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator<<(auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << __LINE__ << ": [" #x "]: ", dump(x) #else #define debug(...) 0 #endif const ll MOD = 1e9 + 7; void DFS(int v, vector<vector<int>>& g, vector<int>& myColor, vector<int>& color, ll& cntA, ll& cntB, ll& diff, bool& twoColor){ if(myColor[v] == 1){ cntA++; }else{ cntB++; } if(color[v]){ if(myColor[v] == 1){ diff++; }else{ diff--; } } for(auto u : g[v]){ if(myColor[u] == 0){ myColor[u] = -myColor[v]; DFS(u, g, myColor, color, cntA, cntB, diff, twoColor); }else if(myColor[u] == myColor[v]){ twoColor = false; } } } long long power(long long a, long long b){ long long res = 1; while(b){ while(!(b&1)){ a = (a * a) % MOD; b>>=1; } res = (res * a) % MOD; b--; } return res; } vector<ll> inv; ll magic(ll a, ll b, ll d){ vector<ll> binomA(a+1); vector<ll> binomB(b+1); binomA[0] = 1; binomB[0] = 1; for(ll i= 1; i <= a; i++){ binomA[i] = (a-i + 1)*inv[i] % MOD; binomA[i] = (binomA[i] * binomA[i-1]) % MOD; } for(ll i= 1; i <= b; i++){ binomB[i] = (b-i + 1)*inv[i] % MOD; binomB[i] = (binomB[i] * binomB[i-1]) % MOD; } ll ans = 0; for(ll k = max(0ll, -d); k <= min(b, a- d); k++){ ans += binomA[k+d] * binomB[k]; ans %= MOD; } return ans; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m; cin>>n>>m; inv.resize(n+1); vector<ll> fact(n+1); fact[0] = 1; for(ll i = 1; i <= n; i++)fact[i] = (fact[i-1] * i) % MOD; ll x = power(fact[n], MOD - 2); for(ll i = n; i >= 1; i--){ inv[i] = (x * fact[i-1]) % MOD; x = (x * i) % MOD; } debug(inv); vector<int> color(n+1); rep(i, n)cin>>color[i+1]; vector<vector<int>> g(n+1); rep(i, m){ int a, b; cin>>a>>b; g[a].push_back(b); g[b].push_back(a); } ll res = 1; vector<int> myColor(n+1, 0); for(int v = 1; v <= n; v++){ if(myColor[v] == 0){ myColor[v] = 1; ll cntA = 0; ll cntB = 0; ll diff = 0; bool twoColor = true; DFS(v, g, myColor, color, cntA, cntB, diff, twoColor); debug(twoColor, cntA, cntB, diff); if(twoColor){ res = (res * magic(cntA, cntB, diff)) % MOD; }else{ res = (res * power(2, cntA + cntB - 1)) % MOD; } } } cout<<res<<"\n"; return 0; } |