#include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> #define MOD 1000000007ll int state[200007]; vector<int> graph[200007]; bool has_neighbor(int v) { for (int u : graph[v]) { if (state[u] == state[v]) { return true; } } // cout << v << " does not have neighbor\n"; return false; } bool dfs(int v, int col, vector<int>& vis, vector<ll>& size, vector<ll>& ones) { if (vis[v] != -1) { return (vis[v] == col); } // cout << v << "\n"; vis[v] = col; size[col]++; ones[col] += state[v]; col = (col + 1) % 2; bool bip = true; for (int u : graph[v]) { bip &= dfs(u, col, vis, size, ones); } return bip; } ll power(ll base, ll ind) { ll out = 1ll; while (ind > 0ll) { if (ind % 2ll == 1ll) { out *= base; out %= MOD; } ind /= 2ll; base *= base; base %= MOD; } return out; } ll fact[200007]; ll choose(ll n, ll k) { ll l = fact[n]; ll m = (fact[n - k] * fact[k]) % MOD; ll out = (l * power(m, MOD - 2)) % MOD; // cout << n << " choose " << k <<" = " << out << "\n"; return out; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; fact[0] = 1; for (ll i = 1; i <= (ll)(n); i++) { fact[i] = fact[i - 1] * i; fact[i] %= MOD; } for (int i = 1; i <= n; i++) { cin >> state[i]; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); } vector<int> vis(n + 1, -1); ll out = 1ll; for (int v = 1; v <= n; v++) { if (vis[v] == -1 && has_neighbor(v)) { // cout << "dfs from " << v << "\n"; vector<ll> size(2, 0); vector<ll> ones(2, 0); bool bip = dfs(v, 1, vis, size, ones); if (!bip) { // 2 ^ size // cout << "not bipartite\n"; out *= power(2ll, size[0] + size[1] - 1ll); out %= MOD; } else { // cout << "bipartite\n"; // cout << size[0] << " " << size[1] << "\n"; // cout << ones[0] << " " << ones[1] << "\n"; ll diff = min(ones[0], ones[1]); ones[0] -= diff; ones[1] -= diff; // cout << ones[0] << " " << ones[1] << "\n"; ll opt = 0; while (ones[0] <= size[0] && ones[1] <= size[1]) { opt += (choose(size[0], ones[0]) * choose(size[1], ones[1])); opt %= MOD; ones[0]++; ones[1]++; } out *= opt; out %= MOD; } } } cout << out << "\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 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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int, int> #define MOD 1000000007ll int state[200007]; vector<int> graph[200007]; bool has_neighbor(int v) { for (int u : graph[v]) { if (state[u] == state[v]) { return true; } } // cout << v << " does not have neighbor\n"; return false; } bool dfs(int v, int col, vector<int>& vis, vector<ll>& size, vector<ll>& ones) { if (vis[v] != -1) { return (vis[v] == col); } // cout << v << "\n"; vis[v] = col; size[col]++; ones[col] += state[v]; col = (col + 1) % 2; bool bip = true; for (int u : graph[v]) { bip &= dfs(u, col, vis, size, ones); } return bip; } ll power(ll base, ll ind) { ll out = 1ll; while (ind > 0ll) { if (ind % 2ll == 1ll) { out *= base; out %= MOD; } ind /= 2ll; base *= base; base %= MOD; } return out; } ll fact[200007]; ll choose(ll n, ll k) { ll l = fact[n]; ll m = (fact[n - k] * fact[k]) % MOD; ll out = (l * power(m, MOD - 2)) % MOD; // cout << n << " choose " << k <<" = " << out << "\n"; return out; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; fact[0] = 1; for (ll i = 1; i <= (ll)(n); i++) { fact[i] = fact[i - 1] * i; fact[i] %= MOD; } for (int i = 1; i <= n; i++) { cin >> state[i]; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); } vector<int> vis(n + 1, -1); ll out = 1ll; for (int v = 1; v <= n; v++) { if (vis[v] == -1 && has_neighbor(v)) { // cout << "dfs from " << v << "\n"; vector<ll> size(2, 0); vector<ll> ones(2, 0); bool bip = dfs(v, 1, vis, size, ones); if (!bip) { // 2 ^ size // cout << "not bipartite\n"; out *= power(2ll, size[0] + size[1] - 1ll); out %= MOD; } else { // cout << "bipartite\n"; // cout << size[0] << " " << size[1] << "\n"; // cout << ones[0] << " " << ones[1] << "\n"; ll diff = min(ones[0], ones[1]); ones[0] -= diff; ones[1] -= diff; // cout << ones[0] << " " << ones[1] << "\n"; ll opt = 0; while (ones[0] <= size[0] && ones[1] <= size[1]) { opt += (choose(size[0], ones[0]) * choose(size[1], ones[1])); opt %= MOD; ones[0]++; ones[1]++; } out *= opt; out %= MOD; } } } cout << out << "\n"; } |