#include <bits/stdc++.h> using namespace std; const int NAX = 2e5 + 5; int lit[NAX]; vector<int> edges[NAX]; int color[NAX]; bool visited[NAX]; bool ok; int cc_size = 0; int half; void dfs(int a) { cc_size += 1; if (color[a] == lit[a]) { half++; } visited[a] = true; for (int b : edges[a]) { if (!visited[b]) { color[b] = color[a] ^ 1; dfs(b); } else if (color[b] == color[a]) { ok = false; } } } const int MOD = 1e9 + 7; int fac[NAX], inv_fac[NAX]; int choose(int a, int b) { return (long long) fac[a] * inv_fac[b] % MOD * inv_fac[a-b] % MOD; // return fac[a] / fac[b] / fac[a-b]; } int my_pow(int a, int b) { int r = 1; while (b) { if (b % 2) { r = (long long) r * a % MOD; } a = (long long) a * a % MOD; b /= 2; } return r; } int my_inv(int a) { return my_pow(a, MOD - 2); } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; fac[0] = inv_fac[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (long long) i * fac[i-1] % MOD; inv_fac[i] = my_inv(fac[i]); } for (int i = 1; i <= n; i++) { cin >> lit[i]; } for (int i = 0; i < m; i++){ int a, b; cin >> a >> b; edges[a].push_back(b); edges[b].push_back(a); } int answer = 1; for (int a = 1; a <= n; a++) { if (!visited[a]) { ok = true; cc_size = half = 0; dfs(a); if (ok) { // bipartite answer = (long long) answer * choose(cc_size, half) % MOD; // cout << "C " << cc_size << " " << half << endl; } else { for (int i = 0; i < cc_size - 1; i++){ answer = 2 * answer % MOD; } } } } cout << answer << 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 | #include <bits/stdc++.h> using namespace std; const int NAX = 2e5 + 5; int lit[NAX]; vector<int> edges[NAX]; int color[NAX]; bool visited[NAX]; bool ok; int cc_size = 0; int half; void dfs(int a) { cc_size += 1; if (color[a] == lit[a]) { half++; } visited[a] = true; for (int b : edges[a]) { if (!visited[b]) { color[b] = color[a] ^ 1; dfs(b); } else if (color[b] == color[a]) { ok = false; } } } const int MOD = 1e9 + 7; int fac[NAX], inv_fac[NAX]; int choose(int a, int b) { return (long long) fac[a] * inv_fac[b] % MOD * inv_fac[a-b] % MOD; // return fac[a] / fac[b] / fac[a-b]; } int my_pow(int a, int b) { int r = 1; while (b) { if (b % 2) { r = (long long) r * a % MOD; } a = (long long) a * a % MOD; b /= 2; } return r; } int my_inv(int a) { return my_pow(a, MOD - 2); } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; fac[0] = inv_fac[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (long long) i * fac[i-1] % MOD; inv_fac[i] = my_inv(fac[i]); } for (int i = 1; i <= n; i++) { cin >> lit[i]; } for (int i = 0; i < m; i++){ int a, b; cin >> a >> b; edges[a].push_back(b); edges[b].push_back(a); } int answer = 1; for (int a = 1; a <= n; a++) { if (!visited[a]) { ok = true; cc_size = half = 0; dfs(a); if (ok) { // bipartite answer = (long long) answer * choose(cc_size, half) % MOD; // cout << "C " << cc_size << " " << half << endl; } else { for (int i = 0; i < cc_size - 1; i++){ answer = 2 * answer % MOD; } } } } cout << answer << endl; } |