// {{{ file: zar.cc | time: 09:14 16.03.2024 #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define each(...) for (auto& __VA_ARGS__) #define rep(i, b, e) for (int i = (b); i <= (e); i++) #define rev(i, b, e) for (int i = (e); i >= (b); i--) #define mp make_pair #define mt make_tuple #define x first #define y second #define pb push_back #define all(x) (x).begin(), (x).end() #define endl '\n' #define tC template<class using namespace std; tC T> using V = vector<T>; tC T1, class T2> using P = pair<T1,T2>; tC T, class C=greater<T>> using PQ = priority_queue<T,V<T>,C>; tC T> int sz(const T& a) { return (int)a.size(); } tC T> bool amin(T& a, T b) { return b < a ? a = b, 1 : 0; } tC T> bool amax(T& a, T b) { return b > a ? a = b, 1 : 0; } using ll = long long; using pii = P<int,int>; using pll = P<ll,ll>; using vi = V<int>; using vl = V<ll>; using vs = V<string>; using vpi = V<pii>; using vpl = V<pll>; const int oo = 1e9 + 1; const ll OO = ll(1e18) + 1; auto now() { return chrono::high_resolution_clock::now().time_since_epoch().count(); } mt19937 rnd(4488); tC T> T rand(T lo, T hi) { return uniform_int_distribution<T>{lo,hi}(rnd); } struct Debug { #ifdef SPONGE tC T>Debug operator<<(const T& x) { cerr<<"\033[1;33m"<<x<<"\033[0m"; #else tC T>Debug operator<<(const T&) { #endif return *this; } } dbg; #define $(x) #x<<'='<<(x)<<' ' void boost() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(15); cout.setf(ios::fixed,ios::floatfield); cerr.precision(15); cerr.setf(ios::fixed,ios::floatfield); } namespace { void solve(); } // }}} int main() { boost(); //rnd.seed(now()); int t=1; //cin>>t; rep(i,1,t){ //cout<<"Case #"<<i<<": "; solve(); } } namespace { const int N=2e5+5; const int mod=1e9+7; int n,m,stan[N],kolor[N]; vi graf[N]; bool is_odd,is_move; int ile[3][2]; ll odp=1; ll fact[N],invfact[N]; ll pot(ll a,ll b) { ll c=1; while(b>0){ if(b%2==1) c=a*c%mod; a=a*a%mod; b/=2; } return c; } ll odw(ll a) { return pot(a,mod-2); } ll choose(int k,int l){ if(k<0 || l>k) return 0; return fact[k]*invfact[l]%mod*invfact[k-l]%mod; } void dfs(int v,int c) { if(kolor[v]){ if(kolor[v]!=c) is_odd=1; } else{ kolor[v]=c; ile[c][stan[v]]++; each(u:graf[v]){ if(stan[u]==stan[v]) is_move=1; dfs(u,3-c); } } } void solve() { cin>>n>>m; rep(i,1,n) cin>>stan[i]; fact[0]=1; invfact[0]=1; rep(i,1,n){ fact[i]=fact[i-1]*i%mod; invfact[i]=odw(fact[i]); } rep(i,1,m){ int u,v; cin>>u>>v; graf[u].pb(v); graf[v].pb(u); } rep(i,1,n) if(!kolor[i]){ is_odd=0; is_move=0; rep(x,1,2) rep(y,0,1) ile[x][y]=0; dfs(i,1); if(is_odd){ int k=0; rep(x,1,2) rep(y,0,1) k+=ile[x][y]; int x=k,xd=0; if(x%2 != (ile[1][1]+ile[2][1])%2) x--; while(x>=0) {xd=(xd+choose(k,x))%mod; x-=2;} odp=odp*ll(xd)%mod; } else if(is_move){ int k1=ile[1][0]+ile[1][1]; int k2=ile[2][0]+ile[2][1]; int x=max(ile[1][0]-k1,ile[2][0]-k2); int xd=0; while(ile[1][0]>=x && ile[2][0]>=x){ xd+=choose(k1,ile[1][0]-x)*choose(k2,ile[2][0]-x)%mod; xd%=mod; x++; } odp=odp*ll(xd)%mod; } } cout<<odp<<endl; } }
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | // {{{ file: zar.cc | time: 09:14 16.03.2024 #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define each(...) for (auto& __VA_ARGS__) #define rep(i, b, e) for (int i = (b); i <= (e); i++) #define rev(i, b, e) for (int i = (e); i >= (b); i--) #define mp make_pair #define mt make_tuple #define x first #define y second #define pb push_back #define all(x) (x).begin(), (x).end() #define endl '\n' #define tC template<class using namespace std; tC T> using V = vector<T>; tC T1, class T2> using P = pair<T1,T2>; tC T, class C=greater<T>> using PQ = priority_queue<T,V<T>,C>; tC T> int sz(const T& a) { return (int)a.size(); } tC T> bool amin(T& a, T b) { return b < a ? a = b, 1 : 0; } tC T> bool amax(T& a, T b) { return b > a ? a = b, 1 : 0; } using ll = long long; using pii = P<int,int>; using pll = P<ll,ll>; using vi = V<int>; using vl = V<ll>; using vs = V<string>; using vpi = V<pii>; using vpl = V<pll>; const int oo = 1e9 + 1; const ll OO = ll(1e18) + 1; auto now() { return chrono::high_resolution_clock::now().time_since_epoch().count(); } mt19937 rnd(4488); tC T> T rand(T lo, T hi) { return uniform_int_distribution<T>{lo,hi}(rnd); } struct Debug { #ifdef SPONGE tC T>Debug operator<<(const T& x) { cerr<<"\033[1;33m"<<x<<"\033[0m"; #else tC T>Debug operator<<(const T&) { #endif return *this; } } dbg; #define $(x) #x<<'='<<(x)<<' ' void boost() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(15); cout.setf(ios::fixed,ios::floatfield); cerr.precision(15); cerr.setf(ios::fixed,ios::floatfield); } namespace { void solve(); } // }}} int main() { boost(); //rnd.seed(now()); int t=1; //cin>>t; rep(i,1,t){ //cout<<"Case #"<<i<<": "; solve(); } } namespace { const int N=2e5+5; const int mod=1e9+7; int n,m,stan[N],kolor[N]; vi graf[N]; bool is_odd,is_move; int ile[3][2]; ll odp=1; ll fact[N],invfact[N]; ll pot(ll a,ll b) { ll c=1; while(b>0){ if(b%2==1) c=a*c%mod; a=a*a%mod; b/=2; } return c; } ll odw(ll a) { return pot(a,mod-2); } ll choose(int k,int l){ if(k<0 || l>k) return 0; return fact[k]*invfact[l]%mod*invfact[k-l]%mod; } void dfs(int v,int c) { if(kolor[v]){ if(kolor[v]!=c) is_odd=1; } else{ kolor[v]=c; ile[c][stan[v]]++; each(u:graf[v]){ if(stan[u]==stan[v]) is_move=1; dfs(u,3-c); } } } void solve() { cin>>n>>m; rep(i,1,n) cin>>stan[i]; fact[0]=1; invfact[0]=1; rep(i,1,n){ fact[i]=fact[i-1]*i%mod; invfact[i]=odw(fact[i]); } rep(i,1,m){ int u,v; cin>>u>>v; graf[u].pb(v); graf[v].pb(u); } rep(i,1,n) if(!kolor[i]){ is_odd=0; is_move=0; rep(x,1,2) rep(y,0,1) ile[x][y]=0; dfs(i,1); if(is_odd){ int k=0; rep(x,1,2) rep(y,0,1) k+=ile[x][y]; int x=k,xd=0; if(x%2 != (ile[1][1]+ile[2][1])%2) x--; while(x>=0) {xd=(xd+choose(k,x))%mod; x-=2;} odp=odp*ll(xd)%mod; } else if(is_move){ int k1=ile[1][0]+ile[1][1]; int k2=ile[2][0]+ile[2][1]; int x=max(ile[1][0]-k1,ile[2][0]-k2); int xd=0; while(ile[1][0]>=x && ile[2][0]>=x){ xd+=choose(k1,ile[1][0]-x)*choose(k2,ile[2][0]-x)%mod; xd%=mod; x++; } odp=odp*ll(xd)%mod; } } cout<<odp<<endl; } } |