#include <bits/stdc++.h> using namespace std; array<long long,200001> c,rr; vector<int> d; array<vector<int>,200001> h; array<int,200001> g; array<long long,32> w; array<long long,32> qq; long long n,m,i,j,a,b,r,q,x,y,hh,ww,k; long long p,z; int main(){ cin >> n >> m; for (i=0;i<n;i++){ cin >> j; c[i]=2*j-1; } for (i=0;i<m;i++){ cin >> a >> b; h[a-1].push_back(b-1); h[b-1].push_back(a-1); } r=1e+9; r+=7; hh=r-2; for (i=0;i<=30;i++){ qq[i]=hh%2; hh/=2; } /*for (i=1;i<=100000;i++){ ww=i; hh=1; for (j=0;j<=30;j++){ if (qq[j]){ hh*=ww; hh%=r; } ww=(ww*ww)%r; } rr[i]=hh; }*/ //for (i=1;i<=5;i++) // cout << rr[i] << ' ' << i << ' ' << i*rr[i]%r << '\n'; //return 0; q=1; for (i=0;i<n;i++){ if (g[i]) continue; j=0; g[i]=1; d={i}; p=0; z=(c[i]+1)/2; while (j<d.size()){ for (auto w:h[d[j]]){ if (g[w]==g[d[j]]){ p=1; continue; } if (g[w]) continue; g[w]=-g[d[j]]; if (c[w]*g[w]>0) z++; d.push_back(w); } j++; } if (p){ for(j=1;j<d.size();j++) { q*=2; q%=r;} } else { if(2*z>d.size()) z=d.size()-z; //cout << z << d.size() << '\n'; for(j=d.size()-z+1;j<=d.size();j++) { q*=j; q%=r;} k=1; for(j=1;j<=z;j++){ k*=j; k%=r; } ww=k; hh=1; for (j=0;j<=30;j++){ if (qq[j]){ hh*=ww; hh%=r; } ww=(ww*ww)%r; } q*=hh; q%=r; } } cout << q << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; array<long long,200001> c,rr; vector<int> d; array<vector<int>,200001> h; array<int,200001> g; array<long long,32> w; array<long long,32> qq; long long n,m,i,j,a,b,r,q,x,y,hh,ww,k; long long p,z; int main(){ cin >> n >> m; for (i=0;i<n;i++){ cin >> j; c[i]=2*j-1; } for (i=0;i<m;i++){ cin >> a >> b; h[a-1].push_back(b-1); h[b-1].push_back(a-1); } r=1e+9; r+=7; hh=r-2; for (i=0;i<=30;i++){ qq[i]=hh%2; hh/=2; } /*for (i=1;i<=100000;i++){ ww=i; hh=1; for (j=0;j<=30;j++){ if (qq[j]){ hh*=ww; hh%=r; } ww=(ww*ww)%r; } rr[i]=hh; }*/ //for (i=1;i<=5;i++) // cout << rr[i] << ' ' << i << ' ' << i*rr[i]%r << '\n'; //return 0; q=1; for (i=0;i<n;i++){ if (g[i]) continue; j=0; g[i]=1; d={i}; p=0; z=(c[i]+1)/2; while (j<d.size()){ for (auto w:h[d[j]]){ if (g[w]==g[d[j]]){ p=1; continue; } if (g[w]) continue; g[w]=-g[d[j]]; if (c[w]*g[w]>0) z++; d.push_back(w); } j++; } if (p){ for(j=1;j<d.size();j++) { q*=2; q%=r;} } else { if(2*z>d.size()) z=d.size()-z; //cout << z << d.size() << '\n'; for(j=d.size()-z+1;j<=d.size();j++) { q*=j; q%=r;} k=1; for(j=1;j<=z;j++){ k*=j; k%=r; } ww=k; hh=1; for (j=0;j<=30;j++){ if (qq[j]){ hh*=ww; hh%=r; } ww=(ww*ww)%r; } q*=hh; q%=r; } } cout << q << '\n'; } |