#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define f first #define s second #define pb push_back #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() using vi = vector<int>; using ll = long long; using ull = unsigned long long; using pll = pair<ll, ll>; using pull = pair<ull, ull>; using vll = vector<ll>; using ii = pair<int, int>; using vvi = vector<vector<int>>; int MOD = 1e9 + 7; const int N = 2e5 + 7; ll sil[N]; ll osil[N]; ll modpow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) { res = res * a % MOD; } a = a * a % MOD; b >>= 1; } return res; } ll binom(int n, int k) { if (k < 0 || k > n) { return 0; } return sil[n] * osil[k] % MOD * osil[n - k] % MOD; } void prep() { sil[0] = 1; for (int i = 1; i < N; i++) { sil[i] = 1ll * sil[i - 1] * i % MOD; } osil[N - 1] = modpow(sil[N - 1], MOD - 2); for (int i = N - 2; i >= 0; i--) { osil[i] = 1ll * osil[i + 1] * (i + 1) % MOD; } } void solve() { int n, m; cin >> n >> m; prep(); vvi G(n); vi tab(n); FOR (i, n) { cin >> tab[i]; } FOR (i, m) { int a, b; cin >> a >> b; a--, b--; G[a].pb(b); G[b].pb(a); } vi cols(n, -1); unordered_map<int, vi> by_col; bool dwudz; function<void(int, int)> dfs = [&](int start, int col ) { cols[start] = col; by_col[col].pb(start); for (int u : G[start]) { if (cols[u] == -1) { dfs(u, col ^ 1); } else { if (cols[u] == cols[start]) { dwudz = false; } } } }; ll spos = 1; FOR (i, n) { if (cols[i] == -1) { dwudz = true; by_col[0].clear(); by_col[1].clear(); dfs(i, 0); if (!dwudz) { int ile = sz(by_col[0]) + sz(by_col[1]); int czar = 0; for (int v : by_col[0]) { czar ^= tab[v]; } for (int v : by_col[1]) { czar ^= tab[v]; } // cout << "NIEDUWIEDLNY " << ile << ' ' << czar << endl; ll teraz = 0; for (int c = czar; c <= ile; c += 2) { teraz += binom(ile, c); teraz %= MOD; } // cout << "TERAZ " << teraz << endl; spos = spos * teraz % MOD; } else { int b1 = 0, c1 = 0, b2 = 0, c2 = 0; for (auto & u : by_col[0]) { if (tab[u] == 0) { b1++; } else { c1++; } } for (auto & u : by_col[1]) { if (tab[u] == 0) { b2++; } else { c2++; } } // cout << "DWUDZIELNY " << b1 << ' ' << c1 << ' ' << b2 << ' ' << c2 << endl; int w_dol = min(c1, c2); int w_gore = min(b1, b2); ll teraz = 0; for (int diff = -w_dol; diff <= w_gore; ++diff) { teraz += 1ll * binom(sz(by_col[0]), c1 + diff) * binom(sz(by_col[1]), c2 + diff); teraz %= MOD; } // cout << "TERAZ " << teraz << endl; spos = spos * teraz % MOD; } } } cout << spos << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int tests = 1; // cin >> tests; for (int test = 1; test <= tests; test++) { solve(); } 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 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | #pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < n; i++) #define f first #define s second #define pb push_back #define all(s) s.begin(), s.end() #define sz(s) (int)s.size() using vi = vector<int>; using ll = long long; using ull = unsigned long long; using pll = pair<ll, ll>; using pull = pair<ull, ull>; using vll = vector<ll>; using ii = pair<int, int>; using vvi = vector<vector<int>>; int MOD = 1e9 + 7; const int N = 2e5 + 7; ll sil[N]; ll osil[N]; ll modpow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) { res = res * a % MOD; } a = a * a % MOD; b >>= 1; } return res; } ll binom(int n, int k) { if (k < 0 || k > n) { return 0; } return sil[n] * osil[k] % MOD * osil[n - k] % MOD; } void prep() { sil[0] = 1; for (int i = 1; i < N; i++) { sil[i] = 1ll * sil[i - 1] * i % MOD; } osil[N - 1] = modpow(sil[N - 1], MOD - 2); for (int i = N - 2; i >= 0; i--) { osil[i] = 1ll * osil[i + 1] * (i + 1) % MOD; } } void solve() { int n, m; cin >> n >> m; prep(); vvi G(n); vi tab(n); FOR (i, n) { cin >> tab[i]; } FOR (i, m) { int a, b; cin >> a >> b; a--, b--; G[a].pb(b); G[b].pb(a); } vi cols(n, -1); unordered_map<int, vi> by_col; bool dwudz; function<void(int, int)> dfs = [&](int start, int col ) { cols[start] = col; by_col[col].pb(start); for (int u : G[start]) { if (cols[u] == -1) { dfs(u, col ^ 1); } else { if (cols[u] == cols[start]) { dwudz = false; } } } }; ll spos = 1; FOR (i, n) { if (cols[i] == -1) { dwudz = true; by_col[0].clear(); by_col[1].clear(); dfs(i, 0); if (!dwudz) { int ile = sz(by_col[0]) + sz(by_col[1]); int czar = 0; for (int v : by_col[0]) { czar ^= tab[v]; } for (int v : by_col[1]) { czar ^= tab[v]; } // cout << "NIEDUWIEDLNY " << ile << ' ' << czar << endl; ll teraz = 0; for (int c = czar; c <= ile; c += 2) { teraz += binom(ile, c); teraz %= MOD; } // cout << "TERAZ " << teraz << endl; spos = spos * teraz % MOD; } else { int b1 = 0, c1 = 0, b2 = 0, c2 = 0; for (auto & u : by_col[0]) { if (tab[u] == 0) { b1++; } else { c1++; } } for (auto & u : by_col[1]) { if (tab[u] == 0) { b2++; } else { c2++; } } // cout << "DWUDZIELNY " << b1 << ' ' << c1 << ' ' << b2 << ' ' << c2 << endl; int w_dol = min(c1, c2); int w_gore = min(b1, b2); ll teraz = 0; for (int diff = -w_dol; diff <= w_gore; ++diff) { teraz += 1ll * binom(sz(by_col[0]), c1 + diff) * binom(sz(by_col[1]), c2 + diff); teraz %= MOD; } // cout << "TERAZ " << teraz << endl; spos = spos * teraz % MOD; } } } cout << spos << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int tests = 1; // cin >> tests; for (int test = 1; test <= tests; test++) { solve(); } return 0; } |