#include <bits/stdc++.h> #define pb push_back using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; const int mod = 1e9+7; const int N = 200003; bool visited[N]; bool visited2[N]; bool color[N]; vi adj[N]; bool stan[N]; vector<long long> pot2; vector<long long> sil; int rozmiar; int odd_jed, odd; int even_jed, even; long long odw(long long a) { long long w = mod-2; long long p = a; long long ans = 1; while(w) { if(w%2) ans = (ans*p) % mod; w /= 2; p = (p*p) % mod; } return ans; } long long dwu(int n, int k) { if(k > n || k < 0) return 0; long long L = sil[n]; long long M = (sil[k]*sil[n-k]) % mod; return (L*odw(M)) % mod; } bool dfs(int x) { //cout << "* " << x << " *"; if(color[x]) { odd++; if(stan[x]) odd_jed++; } else { even++; if(stan[x]) even_jed++; } for(auto u : adj[x]) { if(!visited[u]) { visited[u] = true; color[u] = !color[x]; if(!dfs(u)) return false; } else if(color[x] == color[u]) return false; } return true; } void dfs2(int x) { if(visited2[x]) return; visited2[x] = true; visited[x] = true; rozmiar++; for(auto u : adj[x]) { dfs2(u); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; pot2.pb(1); sil.pb(1); for(long long i = 1; i <= N; i++) { long long k = pot2.back(); pot2.pb((2*k)%mod); long long s = sil.back(); sil.pb((i*s)%mod); } for(int i = 1; i <= n; i++) { int k; cin >> k; stan[i] = k; } for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } // long long ans = 1; for(int i = 1; i <= n; i++) { if(visited[i]) continue; rozmiar = 0; odd_jed = odd = 0; even_jed = even = 0; color[i] = 1; visited[i] = true; bool bip = dfs(i); if(!bip) { dfs2(i); //cout << " bip "; ans = (ans * pot2[rozmiar-1]) % mod; } else { int r = odd_jed - even_jed; //cout << " odd_jed: " << odd_jed << " odd: " << odd << " even_jed: " << even_jed << " even: " << even << "\n"; long long count = 0; for(int k = -r; k <= odd - r; k++) { long long plus = ( dwu(odd, k+r) * dwu(even, k) ) % mod; count = (count + plus) % mod; } ans = (ans * count) % mod; } //cout << ans << " new\n"; } cout << ans << "\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 135 136 137 138 139 140 141 142 143 144 | #include <bits/stdc++.h> #define pb push_back using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; const int mod = 1e9+7; const int N = 200003; bool visited[N]; bool visited2[N]; bool color[N]; vi adj[N]; bool stan[N]; vector<long long> pot2; vector<long long> sil; int rozmiar; int odd_jed, odd; int even_jed, even; long long odw(long long a) { long long w = mod-2; long long p = a; long long ans = 1; while(w) { if(w%2) ans = (ans*p) % mod; w /= 2; p = (p*p) % mod; } return ans; } long long dwu(int n, int k) { if(k > n || k < 0) return 0; long long L = sil[n]; long long M = (sil[k]*sil[n-k]) % mod; return (L*odw(M)) % mod; } bool dfs(int x) { //cout << "* " << x << " *"; if(color[x]) { odd++; if(stan[x]) odd_jed++; } else { even++; if(stan[x]) even_jed++; } for(auto u : adj[x]) { if(!visited[u]) { visited[u] = true; color[u] = !color[x]; if(!dfs(u)) return false; } else if(color[x] == color[u]) return false; } return true; } void dfs2(int x) { if(visited2[x]) return; visited2[x] = true; visited[x] = true; rozmiar++; for(auto u : adj[x]) { dfs2(u); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; pot2.pb(1); sil.pb(1); for(long long i = 1; i <= N; i++) { long long k = pot2.back(); pot2.pb((2*k)%mod); long long s = sil.back(); sil.pb((i*s)%mod); } for(int i = 1; i <= n; i++) { int k; cin >> k; stan[i] = k; } for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } // long long ans = 1; for(int i = 1; i <= n; i++) { if(visited[i]) continue; rozmiar = 0; odd_jed = odd = 0; even_jed = even = 0; color[i] = 1; visited[i] = true; bool bip = dfs(i); if(!bip) { dfs2(i); //cout << " bip "; ans = (ans * pot2[rozmiar-1]) % mod; } else { int r = odd_jed - even_jed; //cout << " odd_jed: " << odd_jed << " odd: " << odd << " even_jed: " << even_jed << " even: " << even << "\n"; long long count = 0; for(int k = -r; k <= odd - r; k++) { long long plus = ( dwu(odd, k+r) * dwu(even, k) ) % mod; count = (count + plus) % mod; } ans = (ans * count) % mod; } //cout << ans << " new\n"; } cout << ans << "\n"; return 0; } |