#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) template<class T> int ssize(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- //https://github.com/tonowak/acmlib using LL = long long; template<int mod> struct modular { int val; modular() { val = 0; } modular(const LL& v) { val = int((-mod <= v && v < mod) ? (int) v : v % mod); if(val < 0) val += mod; } int to_int() { return val; } friend ostream& operator<<(ostream &os, const modular &a) { #ifdef DEBUG constexpr int mx = 1024; for(int y = 1; y <= mx; ++y) if(a * y <= mx) return os << (a * y).val << '/' << y; else if(mod - a * y <= mx) return os << '-' << (mod - a * y).val << '/' << y; #endif return os << a.val; } friend istream& operator>>(istream &is, modular &a) { return is >> a.val; } friend bool operator==(const modular &a, const modular &b) { return a.val == b.val; } friend bool operator!=(const modular &a, const modular &b) { return !(a == b); } friend bool operator<(const modular &a, const modular &b) { return a.val < b.val; } friend bool operator<=(const modular &a, const modular &b) { return a.val <= b.val; } modular operator-() const { return modular(-val); } modular& operator+=(const modular &m) { if((val += m.val) >= mod) val -= mod; return *this; } modular& operator-=(const modular &m) { if((val -= m.val) < 0) val += mod; return *this; } modular& operator*=(const modular &m) { val = int((LL) val * m.val % mod); return *this; } friend modular qpow(modular a, LL n) { if(n == 0) return 1; if(n % 2 == 1) return qpow(a, n - 1) * a; return qpow(a * a, n / 2); } friend modular inv(const modular &a) { assert(a != 0); return qpow(a, mod - 2); } modular& operator/=(const modular &m) { return (*this) *= inv(m); } modular operator++(int) { modular res = (*this); (*this) += 1; return res; } friend modular operator+(modular a, const modular &b) { return a += b; } friend modular operator-(modular a, const modular &b) { return a -= b; } friend modular operator*(modular a, const modular &b) { return a *= b; } friend modular operator/(modular a, const modular &b) { return a /= b; } }; using mint = modular<int(1e9 + 7)>; // using mint = modular<998244353>; struct BinomCoeff { vector<mint> fac, rev; BinomCoeff(int n) { fac = rev = vector<mint>(n + 1, 1); FOR(i, 1, n) fac[i] = fac[i - 1] * i; rev[n] = 1 / fac[n]; for(int i = n; i >= 1; i--) rev[i - 1] = rev[i] * i; } mint operator()(int n, int k) { return fac[n] * rev[n - k] * rev[k]; } }; vector<int> color; vector<vector<int>> graph; int n, m; vector<int> c; vector<int> component; void color_dfs(int u) { component.emplace_back(u); if (color[u] == -1) color[u] = 0; for (int v : graph[u]) { if (color[v] == -1) { color[v] = color[u] ^ 1; color_dfs(v); } } } bool biparite(int source) { component.clear(); color_dfs(source); for (int u : component) { for (int v : graph[u]) { if (color[u] == color[v]) return false; } } return true; } vector<int> match, visited; int t = 0, matching_size = 0; bool matching_dfs(int v) { visited[v] = t; for(int u : graph[v]) if (c[u] == c[v]) { if(match[u] == -1) { match[u] = v; match[v] = u; return true; } } for(int u : graph[v]) if (c[u] == c[v]) { if(visited[match[u]] != t && matching_dfs(match[u])) { match[u] = v; match[v] = u; return true; } } return false; } int turbo_matching() { matching_size = 0; int d = -1; while(d != 0) { d = 0, ++t; for (int u : component) if(match[u] == -1) d += matching_dfs(u); matching_size += d; } return matching_size; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; c = vector<int>(n, 0); int source = 0; REP (i, n) { cin >> c[i]; if (c[i]) source |= (1 << i); } graph.resize(n); REP (i, m) { int a, b; cin >> a >> b; --a, --b; graph[a].emplace_back(b); graph[b].emplace_back(a); } match = visited = color = vector<int>(n, -1); BinomCoeff C(n + 1); mint answer = 1; REP (u, n) { if (color[u] == -1) { if (!biparite(u)) { mint ret = qpow(mint(2), ssize(component) - 1); answer *= ret; debug(ret); } else { bool active = false; array<int, 2> left = {0, 0}, right = {0, 0}; for (int v : component) { if (color[v] == 0) left[c[v]]++; else right[c[v]]++; for (int w : graph[v]) { if (c[v] == c[w]) { active = true; } } } if (!active) continue; if (left[0] + left[1] > right[0] + right[1]) swap(left, right); debug(left); debug(right); int k = 0; REP (b, 2) { if (left[b] > right[b]) continue; int take = min(left[b], right[b]); int ans = right[b ^ 1] + take; k = max(ans, k); } debug(component, k); mint ret = C(ssize(component), k); answer *= ret; debug(ret); } } } cout << answer << '\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 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) template<class T> int ssize(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- //https://github.com/tonowak/acmlib using LL = long long; template<int mod> struct modular { int val; modular() { val = 0; } modular(const LL& v) { val = int((-mod <= v && v < mod) ? (int) v : v % mod); if(val < 0) val += mod; } int to_int() { return val; } friend ostream& operator<<(ostream &os, const modular &a) { #ifdef DEBUG constexpr int mx = 1024; for(int y = 1; y <= mx; ++y) if(a * y <= mx) return os << (a * y).val << '/' << y; else if(mod - a * y <= mx) return os << '-' << (mod - a * y).val << '/' << y; #endif return os << a.val; } friend istream& operator>>(istream &is, modular &a) { return is >> a.val; } friend bool operator==(const modular &a, const modular &b) { return a.val == b.val; } friend bool operator!=(const modular &a, const modular &b) { return !(a == b); } friend bool operator<(const modular &a, const modular &b) { return a.val < b.val; } friend bool operator<=(const modular &a, const modular &b) { return a.val <= b.val; } modular operator-() const { return modular(-val); } modular& operator+=(const modular &m) { if((val += m.val) >= mod) val -= mod; return *this; } modular& operator-=(const modular &m) { if((val -= m.val) < 0) val += mod; return *this; } modular& operator*=(const modular &m) { val = int((LL) val * m.val % mod); return *this; } friend modular qpow(modular a, LL n) { if(n == 0) return 1; if(n % 2 == 1) return qpow(a, n - 1) * a; return qpow(a * a, n / 2); } friend modular inv(const modular &a) { assert(a != 0); return qpow(a, mod - 2); } modular& operator/=(const modular &m) { return (*this) *= inv(m); } modular operator++(int) { modular res = (*this); (*this) += 1; return res; } friend modular operator+(modular a, const modular &b) { return a += b; } friend modular operator-(modular a, const modular &b) { return a -= b; } friend modular operator*(modular a, const modular &b) { return a *= b; } friend modular operator/(modular a, const modular &b) { return a /= b; } }; using mint = modular<int(1e9 + 7)>; // using mint = modular<998244353>; struct BinomCoeff { vector<mint> fac, rev; BinomCoeff(int n) { fac = rev = vector<mint>(n + 1, 1); FOR(i, 1, n) fac[i] = fac[i - 1] * i; rev[n] = 1 / fac[n]; for(int i = n; i >= 1; i--) rev[i - 1] = rev[i] * i; } mint operator()(int n, int k) { return fac[n] * rev[n - k] * rev[k]; } }; vector<int> color; vector<vector<int>> graph; int n, m; vector<int> c; vector<int> component; void color_dfs(int u) { component.emplace_back(u); if (color[u] == -1) color[u] = 0; for (int v : graph[u]) { if (color[v] == -1) { color[v] = color[u] ^ 1; color_dfs(v); } } } bool biparite(int source) { component.clear(); color_dfs(source); for (int u : component) { for (int v : graph[u]) { if (color[u] == color[v]) return false; } } return true; } vector<int> match, visited; int t = 0, matching_size = 0; bool matching_dfs(int v) { visited[v] = t; for(int u : graph[v]) if (c[u] == c[v]) { if(match[u] == -1) { match[u] = v; match[v] = u; return true; } } for(int u : graph[v]) if (c[u] == c[v]) { if(visited[match[u]] != t && matching_dfs(match[u])) { match[u] = v; match[v] = u; return true; } } return false; } int turbo_matching() { matching_size = 0; int d = -1; while(d != 0) { d = 0, ++t; for (int u : component) if(match[u] == -1) d += matching_dfs(u); matching_size += d; } return matching_size; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; c = vector<int>(n, 0); int source = 0; REP (i, n) { cin >> c[i]; if (c[i]) source |= (1 << i); } graph.resize(n); REP (i, m) { int a, b; cin >> a >> b; --a, --b; graph[a].emplace_back(b); graph[b].emplace_back(a); } match = visited = color = vector<int>(n, -1); BinomCoeff C(n + 1); mint answer = 1; REP (u, n) { if (color[u] == -1) { if (!biparite(u)) { mint ret = qpow(mint(2), ssize(component) - 1); answer *= ret; debug(ret); } else { bool active = false; array<int, 2> left = {0, 0}, right = {0, 0}; for (int v : component) { if (color[v] == 0) left[c[v]]++; else right[c[v]]++; for (int w : graph[v]) { if (c[v] == c[w]) { active = true; } } } if (!active) continue; if (left[0] + left[1] > right[0] + right[1]) swap(left, right); debug(left); debug(right); int k = 0; REP (b, 2) { if (left[b] > right[b]) continue; int take = min(left[b], right[b]); int ans = right[b ^ 1] + take; k = max(ans, k); } debug(component, k); mint ret = C(ssize(component), k); answer *= ret; debug(ret); } } } cout << answer << '\n'; } |