#include <bits/stdc++.h> using namespace std; long long odw(long long a,long long p) { long long u=1,x=0,w=a,z=p; while(w>0) { if(w<z) { long long pom=u; u=x; x=pom; pom=w; w=z; z=pom; } long long q=w/z; u-=x*q; w-=q*z; } while(x<0) x+=p; if(x>p) x%=p; return x; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; vector<bool> c(n); long long p=1'000'000'007; for(int i=0;i<n;i++) { char x; cin>>x; if(x=='1') c[i]=1; else c[i]=0; } vector<vector<int>> G(n); for(int i=0;i<m;i++) { int x,y; cin>>x>>y; x--;y--; G[x].push_back(y); G[y].push_back(x); } vector<bool> visited(n,0); long long ans=1; for(int s=0;s<n;s++) { if(visited[s]==1) continue; if(G[s].size()==0) continue; //cout<<"zaczynamy od "<<s<<endl; queue<int> q; q.push(s); visited[s]=1; vector<int> d(n,-1); d[s]=0; int a=1,b=0,dif=0; if(c[s]==1) dif=1; bool bip=true; while(q.empty()==false) { int u=q.front(); //cout<<u<<endl; q.pop(); for(int i=0;i<G[u].size();i++) { if(d[G[u][i]]==-1) { if(d[u]==0) { d[G[u][i]]=1; b++; if(c[G[u][i]]==1) dif--; } else { d[G[u][i]]=0; a++; if(c[G[u][i]]==1) dif++; } q.push(G[u][i]); visited[G[u][i]]=1; } else if(d[G[u][i]]==d[u]) bip=false; } } if(bip==true) { //cout<<"Hello"<<endl; if(dif<0) { int pom=a; a=b; b=pom; dif*=-1; } long long c1=1; long long c2=1; for(int i=0;i<dif;i++) { c2*=(a-i); c2%=p; c2*=odw((long long) (i+1),p); c2%=p; } long long c=c2; for(int i=1;i<=min(a-dif,b);i++) { c1*=(b+1-i); c1%=p; c1*=odw((long long) i,p); c1%=p; c2*=(a+1-i-dif); c2%=p; c2*=odw((long long) (i+dif),p); c2%=p; c+=c1*c2; c%=p; } ans*=c; ans%=p; } else { long long x=1; for(int i=0;i<a+b-1;i++) { x*=2; x%=p; } ans*=x; ans%=p; } } cout<<ans<<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 | #include <bits/stdc++.h> using namespace std; long long odw(long long a,long long p) { long long u=1,x=0,w=a,z=p; while(w>0) { if(w<z) { long long pom=u; u=x; x=pom; pom=w; w=z; z=pom; } long long q=w/z; u-=x*q; w-=q*z; } while(x<0) x+=p; if(x>p) x%=p; return x; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; vector<bool> c(n); long long p=1'000'000'007; for(int i=0;i<n;i++) { char x; cin>>x; if(x=='1') c[i]=1; else c[i]=0; } vector<vector<int>> G(n); for(int i=0;i<m;i++) { int x,y; cin>>x>>y; x--;y--; G[x].push_back(y); G[y].push_back(x); } vector<bool> visited(n,0); long long ans=1; for(int s=0;s<n;s++) { if(visited[s]==1) continue; if(G[s].size()==0) continue; //cout<<"zaczynamy od "<<s<<endl; queue<int> q; q.push(s); visited[s]=1; vector<int> d(n,-1); d[s]=0; int a=1,b=0,dif=0; if(c[s]==1) dif=1; bool bip=true; while(q.empty()==false) { int u=q.front(); //cout<<u<<endl; q.pop(); for(int i=0;i<G[u].size();i++) { if(d[G[u][i]]==-1) { if(d[u]==0) { d[G[u][i]]=1; b++; if(c[G[u][i]]==1) dif--; } else { d[G[u][i]]=0; a++; if(c[G[u][i]]==1) dif++; } q.push(G[u][i]); visited[G[u][i]]=1; } else if(d[G[u][i]]==d[u]) bip=false; } } if(bip==true) { //cout<<"Hello"<<endl; if(dif<0) { int pom=a; a=b; b=pom; dif*=-1; } long long c1=1; long long c2=1; for(int i=0;i<dif;i++) { c2*=(a-i); c2%=p; c2*=odw((long long) (i+1),p); c2%=p; } long long c=c2; for(int i=1;i<=min(a-dif,b);i++) { c1*=(b+1-i); c1%=p; c1*=odw((long long) i,p); c1%=p; c2*=(a+1-i-dif); c2%=p; c2*=odw((long long) (i+dif),p); c2%=p; c+=c1*c2; c%=p; } ans*=c; ans%=p; } else { long long x=1; for(int i=0;i<a+b-1;i++) { x*=2; x%=p; } ans*=x; ans%=p; } } cout<<ans<<endl; } |