#include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; /* */ const long long MD = 1e9+7; template<long long MOD=MD> struct mdint { int d; mdint () {d=0;} mdint (long long _d) : d(_d%MOD){ if(d<0) d+=MOD; }; friend mdint& operator+=(mdint& a, const mdint& o) { a.d+=o.d; if(a.d>=MOD) a.d-=MOD; return a; } friend mdint& operator-=(mdint& a, const mdint& o) { a.d-=o.d; if(a.d<0) a.d+=MOD; return a; } friend mdint& operator*=(mdint& a, const mdint& o) { return a = mdint((ll)a.d*o.d); } mdint operator*(const mdint& o) const { mdint res = *this; res*=o; return res; } mdint operator+(const mdint& o) const { mdint res = *this; res+=o; return res; } mdint operator-(const mdint& o) const { mdint res = *this; res-=o; return res; } mdint operator^(long long b) const { mdint tmp = 1; mdint power = *this; while(b) { if(b&1) { tmp = tmp*power; } power = power*power; b/=2; } return tmp; } friend mdint operator/=(mdint& a, const mdint& o) { a *= (o^(MOD-2)); return a; } mdint operator/(const mdint& o) { mdint res = *this; res/=o; return res; } bool operator==(const mdint& o) { return d==o.d;} bool operator!=(const mdint& o) { return d!=o.d;} friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;} friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;} }; using mint = mdint<MD>; const int mxF = 2e6+2; mint fact[mxF], ifact[mxF]; mint ncr(int a, int b) { if(b>a or a<0 or b<0) return 0; return fact[a]*ifact[b]*ifact[a-b]; } void precomp() { fact[0]=1; for(int i=1;i<mxF;++i) { fact[i]=fact[i-1]*i; } ifact[mxF-1] = mint(1)/fact[mxF-1]; for(int i=mxF-2;i>=0;--i) { ifact[i]=ifact[i+1]*(i+1); } } int main() { precomp(); ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m; cin >> n >> m; vector<bool> col(n),vis(n); vi c(n); for(auto& i : c) cin >> i; vvi adj(n); for(int i=0;i<m;++i) { int u,v; cin >> u >> v; --u,--v; adj[u].push_back(v); adj[v].push_back(u); } mint ans=1; for(int i=0;i<n;++i) if(!vis[i]) { int a[2][2] = {}; bool bipart=1; auto dfs = [&](auto&& self, int at) -> void { vis[at]=1; a[c[at]][col[at]]++; for(int to : adj[at]) { if(vis[to]) { if(col[to]==col[at]) bipart=0; } else { col[to]=!col[at]; self(self,to); } } }; dfs(dfs,i); if(bipart) { int total = a[0][0]+a[0][1]+a[1][0]+a[1][1]; int asz= a[0][0]+a[1][0]; int bsz = total-asz; mint cur=0; for(int exch=-total;exch<=total;++exch) { cur+=ncr(asz,a[0][0]+exch)*ncr(bsz,a[0][1]+exch); } ans*=cur; } else { int zs = a[0][0]+a[0][1]; int os = a[1][0]+a[1][1]; // everything is possible with the right parity? ans*=mint(2)^(zs+os-1); } } cout << ans << '\n'; // 2 3 6 10 35 70 126 }
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 "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; /* */ const long long MD = 1e9+7; template<long long MOD=MD> struct mdint { int d; mdint () {d=0;} mdint (long long _d) : d(_d%MOD){ if(d<0) d+=MOD; }; friend mdint& operator+=(mdint& a, const mdint& o) { a.d+=o.d; if(a.d>=MOD) a.d-=MOD; return a; } friend mdint& operator-=(mdint& a, const mdint& o) { a.d-=o.d; if(a.d<0) a.d+=MOD; return a; } friend mdint& operator*=(mdint& a, const mdint& o) { return a = mdint((ll)a.d*o.d); } mdint operator*(const mdint& o) const { mdint res = *this; res*=o; return res; } mdint operator+(const mdint& o) const { mdint res = *this; res+=o; return res; } mdint operator-(const mdint& o) const { mdint res = *this; res-=o; return res; } mdint operator^(long long b) const { mdint tmp = 1; mdint power = *this; while(b) { if(b&1) { tmp = tmp*power; } power = power*power; b/=2; } return tmp; } friend mdint operator/=(mdint& a, const mdint& o) { a *= (o^(MOD-2)); return a; } mdint operator/(const mdint& o) { mdint res = *this; res/=o; return res; } bool operator==(const mdint& o) { return d==o.d;} bool operator!=(const mdint& o) { return d!=o.d;} friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;} friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;} }; using mint = mdint<MD>; const int mxF = 2e6+2; mint fact[mxF], ifact[mxF]; mint ncr(int a, int b) { if(b>a or a<0 or b<0) return 0; return fact[a]*ifact[b]*ifact[a-b]; } void precomp() { fact[0]=1; for(int i=1;i<mxF;++i) { fact[i]=fact[i-1]*i; } ifact[mxF-1] = mint(1)/fact[mxF-1]; for(int i=mxF-2;i>=0;--i) { ifact[i]=ifact[i+1]*(i+1); } } int main() { precomp(); ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m; cin >> n >> m; vector<bool> col(n),vis(n); vi c(n); for(auto& i : c) cin >> i; vvi adj(n); for(int i=0;i<m;++i) { int u,v; cin >> u >> v; --u,--v; adj[u].push_back(v); adj[v].push_back(u); } mint ans=1; for(int i=0;i<n;++i) if(!vis[i]) { int a[2][2] = {}; bool bipart=1; auto dfs = [&](auto&& self, int at) -> void { vis[at]=1; a[c[at]][col[at]]++; for(int to : adj[at]) { if(vis[to]) { if(col[to]==col[at]) bipart=0; } else { col[to]=!col[at]; self(self,to); } } }; dfs(dfs,i); if(bipart) { int total = a[0][0]+a[0][1]+a[1][0]+a[1][1]; int asz= a[0][0]+a[1][0]; int bsz = total-asz; mint cur=0; for(int exch=-total;exch<=total;++exch) { cur+=ncr(asz,a[0][0]+exch)*ncr(bsz,a[0][1]+exch); } ans*=cur; } else { int zs = a[0][0]+a[0][1]; int os = a[1][0]+a[1][1]; // everything is possible with the right parity? ans*=mint(2)^(zs+os-1); } } cout << ans << '\n'; // 2 3 6 10 35 70 126 } |