#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif constexpr int mod = 1'000'000'007; int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } int sub(int a, int b) { return add(a, mod - b); } int mul(int a, int b) { return int(a * LL(b) % mod); } int powi(int a, int b) { for(int ret = 1;; b /= 2) { if(b == 0) return ret; if(b & 1) ret = mul(ret, a); a = mul(a, a); } } int inv(int x) { return powi(x, mod - 2); } // END HASH struct BinomCoeff { vector<int> fac, rev; BinomCoeff(int n) { fac = rev = vector(n + 1, 1); FOR(i, 1, n) fac[i] = mul(fac[i - 1], i); rev[n] = inv(fac[n]); for(int i = n; i > 0; --i) rev[i - 1] = mul(rev[i], i); } int operator()(int n, int k) { if (n < 0 || k < 0 || k > n) return 0; return mul(fac[n], mul(rev[n - k], rev[k])); } }; int brute(vector<vector<int>> graph, vector<int> state) { vector<pair<int, int>> edges; int n = ssize(graph); REP (v, n) for (int u : graph[v]) if (v < u) edges.emplace_back(v, u); set<vector<int>> vis; function<void()> rek = [&]() { if (vis.count(state)) return; vis.emplace(state); for (auto [l, r] : edges) { if (state[l] == state[r]) { state[l] ^= 1; state[r] ^= 1; rek(); state[l] ^= 1; state[r] ^= 1; } } }; rek(); return ssize(vis); } int calc(vector<vector<int>> graph, vector<int> state) { int n = ssize(graph); vector<int> par(n), odw(n); function<void(int)> dfs = [&](int v) { odw[v] = 1; for (int u : graph[v]) if (!odw[u]) { par[u] = par[v] ^ 1; dfs(u); } }; dfs(0); bool ok = true; REP (i, n) for (int u : graph[i]) if (par[i] == par[u]) ok = false; if (ok) { bool is_moveable = false; REP (i, n) for (int u : graph[i]) if (state[i] == state[u]) is_moveable = true; if (!is_moveable) return 1; vector zli(2, vector(2, 0)); REP (i, n) ++zli[state[i]][par[i]]; BinomCoeff bin(n); int ans = 0; debug(zli); int w = min(zli[0][0], zli[0][1]); zli[0][0] -= w; zli[0][1] -= w; zli[1][0] += w; zli[1][1] += w; if (zli[0][0]) { swap(zli[0][0], zli[0][1]); swap(zli[1][0], zli[1][1]); } // zli[0][0] == 0 REP (i, min(zli[1][0], zli[1][1]) + 1) { // ile par zer int even = zli[0][0] + zli[1][0]; int odd = zli[0][1] + zli[1][1]; ans = add(ans, mul(bin(even, i), bin(odd, zli[0][1] + i))); } return ans; // return brute(graph, state); } else { vector<int> zli(2); REP (i, n) zli[state[i]] ^= 1; int ret = 0; BinomCoeff bin(n); for (int i = zli[0]; i <= n; i += 2) ret = add(ret, bin(n, i)); return ret; } } int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector<int> state(n); REP (i, n) cin >> state[i]; vector<vector<int>> graph(n); REP (i, m) { int u, v; cin >> u >> v; --u; --v; graph[u].emplace_back(v); graph[v].emplace_back(u); } int ans = 1; vector<int> odw(n); REP (i, n) { if (odw[i]) continue; int licz = 0; map<int, int> mp; vector<int> nstate; vector<vector<int>> ngraph; function<void(int)> dfs = [&](int v) { ngraph.emplace_back(vector<int>()); nstate.emplace_back(state[v]); mp[v] = licz++; odw[v] = 1; for (int u : graph[v]) if (!odw[u]) dfs(u); for (int u : graph[v]) ngraph[mp[v]].emplace_back(mp[u]); }; dfs(i); ans = mul(ans, calc(ngraph, nstate)); } cout << ans << '\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 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 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif constexpr int mod = 1'000'000'007; int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } int sub(int a, int b) { return add(a, mod - b); } int mul(int a, int b) { return int(a * LL(b) % mod); } int powi(int a, int b) { for(int ret = 1;; b /= 2) { if(b == 0) return ret; if(b & 1) ret = mul(ret, a); a = mul(a, a); } } int inv(int x) { return powi(x, mod - 2); } // END HASH struct BinomCoeff { vector<int> fac, rev; BinomCoeff(int n) { fac = rev = vector(n + 1, 1); FOR(i, 1, n) fac[i] = mul(fac[i - 1], i); rev[n] = inv(fac[n]); for(int i = n; i > 0; --i) rev[i - 1] = mul(rev[i], i); } int operator()(int n, int k) { if (n < 0 || k < 0 || k > n) return 0; return mul(fac[n], mul(rev[n - k], rev[k])); } }; int brute(vector<vector<int>> graph, vector<int> state) { vector<pair<int, int>> edges; int n = ssize(graph); REP (v, n) for (int u : graph[v]) if (v < u) edges.emplace_back(v, u); set<vector<int>> vis; function<void()> rek = [&]() { if (vis.count(state)) return; vis.emplace(state); for (auto [l, r] : edges) { if (state[l] == state[r]) { state[l] ^= 1; state[r] ^= 1; rek(); state[l] ^= 1; state[r] ^= 1; } } }; rek(); return ssize(vis); } int calc(vector<vector<int>> graph, vector<int> state) { int n = ssize(graph); vector<int> par(n), odw(n); function<void(int)> dfs = [&](int v) { odw[v] = 1; for (int u : graph[v]) if (!odw[u]) { par[u] = par[v] ^ 1; dfs(u); } }; dfs(0); bool ok = true; REP (i, n) for (int u : graph[i]) if (par[i] == par[u]) ok = false; if (ok) { bool is_moveable = false; REP (i, n) for (int u : graph[i]) if (state[i] == state[u]) is_moveable = true; if (!is_moveable) return 1; vector zli(2, vector(2, 0)); REP (i, n) ++zli[state[i]][par[i]]; BinomCoeff bin(n); int ans = 0; debug(zli); int w = min(zli[0][0], zli[0][1]); zli[0][0] -= w; zli[0][1] -= w; zli[1][0] += w; zli[1][1] += w; if (zli[0][0]) { swap(zli[0][0], zli[0][1]); swap(zli[1][0], zli[1][1]); } // zli[0][0] == 0 REP (i, min(zli[1][0], zli[1][1]) + 1) { // ile par zer int even = zli[0][0] + zli[1][0]; int odd = zli[0][1] + zli[1][1]; ans = add(ans, mul(bin(even, i), bin(odd, zli[0][1] + i))); } return ans; // return brute(graph, state); } else { vector<int> zli(2); REP (i, n) zli[state[i]] ^= 1; int ret = 0; BinomCoeff bin(n); for (int i = zli[0]; i <= n; i += 2) ret = add(ret, bin(n, i)); return ret; } } int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector<int> state(n); REP (i, n) cin >> state[i]; vector<vector<int>> graph(n); REP (i, m) { int u, v; cin >> u >> v; --u; --v; graph[u].emplace_back(v); graph[v].emplace_back(u); } int ans = 1; vector<int> odw(n); REP (i, n) { if (odw[i]) continue; int licz = 0; map<int, int> mp; vector<int> nstate; vector<vector<int>> ngraph; function<void(int)> dfs = [&](int v) { ngraph.emplace_back(vector<int>()); nstate.emplace_back(state[v]); mp[v] = licz++; odw[v] = 1; for (int u : graph[v]) if (!odw[u]) dfs(u); for (int u : graph[v]) ngraph[mp[v]].emplace_back(mp[u]); }; dfs(i); ans = mul(ans, calc(ngraph, nstate)); } cout << ans << '\n'; } |