#include <bits/stdc++.h> using namespace std; // loops #define FOR_3(i, a, b) for (int i = (a), bb##i = (b); i <= bb##i; ++i) #define FORR_3(i, a, b) for (int i = (b), aa##i = (a); i >= aa##i; --i) #define FOR_2(i, n) FOR_3(i, 0, (n)-1) #define FORR_2(i, n) FORR_3(i, 0, (n)-1) #define FOR_1(n) FOR_2(_i##__LINE__, n) #define FOR_0() for (;;) #define OVERLOAD(_0, _1, _2, _3, NAME, ...) NAME #define FOR(...) \ OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FOR_3, FOR_2, FOR_1, FOR_0) \ (__VA_ARGS__) #define FORR(...) \ OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FORR_3, FORR_2, FORR_1, FOR_0) \ (__VA_ARGS__) // in - expr style template <class T> T read() { T x; cin >> x; return x; } #define II read<int>() #define ILL read<ll>() #define ISTR read<string>() // in - macro style void _I() {} template <class ARG, class... ARGS> void _I(ARG &&arg, ARGS &&...args) { cin >> arg; _I(forward<ARGS>(args)...); } #define I(...) _I(__VA_ARGS__); // out template <class T> void _print(T x, ostream &os = cout) { os << x; bool space = true; if constexpr (is_same_v<T, char>) if (x == '\n') space = false; if (space) os << " "; } // template <class T> void _print(const vector<T> &d, ostream &os = cout) { // os << '['; // for (auto &e : d) // os << e << " "; // os << ']'; // } void _O() {} template <class ARG, class... ARGS> void _O(ARG &&arg, ARGS &&...args) { _print(arg); _O(forward<ARGS>(args)...); } #define O(...) _O(__VA_ARGS__ __VA_OPT__(, ) '\n'); #define OO(...) _O(__VA_ARGS__); // err const int _indentSize = 2; string _indent; void _E() { cerr << endl; } template <class ARG, class... ARGS> void _E(ARG &&arg, ARGS &&...args) { _print(forward<ARG>(arg), cerr); _E(std::forward<ARGS>(args)...); } #define ERR(...) \ { \ cerr << _indent << "[" << __LINE__ << "] "; \ _E(__VA_ARGS__); \ } #define _V5(x) #x, "==", (x) #define _V4(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V5(__VA_ARGS__)) #define _V3(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V4(__VA_ARGS__)) #define _V2(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V3(__VA_ARGS__)) #define _V1(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V2(__VA_ARGS__)) #define ERRV(...) ERR(_V1(__VA_ARGS__)) #define INDENT auto _indenter = Indenter(); struct Indenter { Indenter() { FOR(_indentSize) _indent.push_back(' '); } ~Indenter() { FOR(_indentSize) _indent.pop_back(); } }; #ifdef DEBUG #define FAIL(str) \ { \ cerr << "ASSERT FAILED " << str << " " << __FILE__ << ':' << __LINE__ \ << endl; \ abort(); \ } #else #define FAIL(x) \ {} #endif // asserts #define CHECK(cond) \ if (!(cond)) \ FAIL(#cond) #define CHECK_OP(op, a, b, res) \ if (!(res)) \ FAIL(#a " " op " " #b " (" + to_string(a) + " vs " + to_string(b) + ")") #define CHECK_EQ(a, b) CHECK_OP("==", a, b, (a) == (b)) #define CHECK_NE(a, b) CHECK_OP("!=", a, b, (a) != (b)) #define CHECK_LE(a, b) CHECK_OP("<=", a, b, (a) <= (b)) #define CHECK_GE(a, b) CHECK_OP(">=", a, b, (a) >= (b)) #define CHECK_LT(a, b) CHECK_OP("<", a, b, (a) < (b)) #define CHECK_GT(a, b) CHECK_OP(">", a, b, (a) > (b)) #define CHECK_RANGE(x, a, b) \ if (!(a <= x && x < b)) \ FAIL(#x " in [" #a "," #b ")") // #define SZ(v) ((int)(v).size()) using pii = pair<int, int>; // template <class T, class E> T fpow(T a, E exp) { T r = 1; while (exp) { if (exp & 1) { r *= a; --exp; } else { exp >>= 1; a *= a; } } return r; } //////////////////////////////////////////////////////////////////////////////// template <int MOD> class Modulo { public: using H = int; H h = 0; public: Modulo() = default; Modulo(H x) : h((x + MOD) % MOD){CHECK_GE(x + MOD, 0)} Modulo(const Modulo &) = default; public: Modulo &operator+=(const Modulo &o) { h += o.h; if (h >= MOD) h -= MOD; return *this; } Modulo &operator-=(const Modulo &o) { h = MOD + h - o.h; if (h >= MOD) h -= MOD; return *this; } Modulo &operator*=(const Modulo &o) { h = (1ULL * h * o.h) % MOD; return *this; } Modulo &operator/=(const Modulo &o) { const auto TOTIENT = MOD - 1; *this *= fpow(o, TOTIENT - 1); return *this; } bool operator==(const Modulo &o) const { return h == o.h; } bool operator!=(const Modulo &o) const { return h != o.h; } Modulo operator+(const Modulo &o) const { return Modulo(*this) += o; } Modulo operator-(const Modulo &o) const { return Modulo(*this) -= o; } Modulo operator*(const Modulo &o) const { return Modulo(*this) *= o; } Modulo operator/(const Modulo &o) const { return Modulo(*this) /= o; } Modulo &operator--() { if (h == 0) h = MOD; --h; return *this; } Modulo &operator++() { ++h; if (h == MOD) h = 0; return *this; } Modulo operator--(int) { auto m = *this; --(*this); return m; } Modulo operator++(int) { auto m = *this; ++(*this); return m; } explicit operator H() const { return h; } }; // snippets/modulo.hpp //////////////////////////////////////////////////////////////////////////////// // // snippets/binomial.hpp // template <class VAL> struct Binomial { int n = 0; int k = 0; VAL val = (VAL)1; public: Binomial() = default; Binomial(int nn, int kk) { (*this)(nn, kk); } // hack Binomial(int nn, int kk, const VAL &vv) : n(nn), k(kk), val(vv) {} operator const VAL &() const { return val; } VAL operator()(int nn, int kk) { if (outside(nn, kk)) return VAL(0); while (k > kk) move_l(); while (n > nn) move_u(); while (n < nn) move_d(); while (k < kk) move_r(); return val; } static bool outside(int nn, int kk) { return nn < 0 || kk < 0 || kk > nn; } void move_l() { --k; val *= VAL(k + 1); val /= VAL(n - k); } void move_r() { ++k; val *= VAL(n - k + 1); val /= VAL(k); } void move_d() { ++n; val *= VAL(n); val /= VAL(n - k); } void move_u() { --n; val *= VAL(n - k + 1); val /= VAL(n + 1); } }; using F = Modulo<int(1e9) + 7>; // using F = Modulo<998'244'353>; ostream &operator<<(ostream &os, const F &modulo) { return os << modulo.h; } using B = Binomial<F>; //// using ll = long long; // struct In { int n, m; } in; struct V { int bit; int parity = -1; vector<int> edges; }; vector<V> vs; vector<char> vis; struct Data { int num = 0; int num_per_parity[2] = {}; int num_bit[2] = {}; int num_bit_per_parity[2][2] = {}; int num_edges = 0; int odd = false; bool have_move = false; }; Data dfs_data; void dfs(int v, int parity) { if (vs[v].parity == -1) { vs[v].parity = parity; } else { if (vs[v].parity != parity) { dfs_data.odd = true; } } if (vis[v]) return; vis[v] = 1; dfs_data.num++; dfs_data.num_per_parity[parity]++; dfs_data.num_bit[vs[v].bit]++; dfs_data.num_bit_per_parity[parity][vs[v].bit]++; for (int u : vs[v].edges) { dfs_data.num_edges++; if (vs[v].bit == vs[u].bit) dfs_data.have_move = true; dfs(u, parity ^ 1); } } void read_input() { in.n = II; in.m = II; FOR(in.n) { int bit = II; vs.emplace_back(); vs.back().bit = bit; } FOR(in.m) { int ai = II - 1; int bi = II - 1; vs[ai].edges.push_back(bi); vs[bi].edges.push_back(ai); } } void generate_input() { in.n = 200'000; in.m = 0; FOR(in.n) { int bit = rand()%2; vs.emplace_back(); vs.back().bit = bit; } } int main() { ios::sync_with_stdio(0); cin.tie(0); read_input(); // generate_input(); vis.resize(in.n); F result = (F)1; FOR(v, in.n) { dfs_data = Data{}; dfs(v, 0); dfs_data.num_edges /= 2; if (dfs_data.num == 0) continue; // ERRV(v + 1, dfs_data.odd, dfs_data.num); if (dfs_data.odd) { if (dfs_data.have_move) { F cand = 0; B binomial; auto mod2 = dfs_data.num_bit[1] % 2; FOR(num1, 0, dfs_data.num) { if (num1 % 2 == mod2) cand += binomial(dfs_data.num, num1); } result *= cand; } } else { // INDENT; auto diff = dfs_data.num_bit_per_parity[1][1] - dfs_data.num_bit_per_parity[0][1]; auto binomial0 = B(); auto binomial1 = B(); F cand = (F)0; FOR(num1_parity0, 0, dfs_data.num_per_parity[0]) { // INDENT; auto num1_parity1 = num1_parity0 + diff; if (num1_parity1 > dfs_data.num_per_parity[1]) break; F part = binomial0(dfs_data.num_per_parity[0], num1_parity0) * binomial1(dfs_data.num_per_parity[1], num1_parity1); // ERRV(part); cand += part; } // ERRV(cand); result *= cand; } } O(result); 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 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | #include <bits/stdc++.h> using namespace std; // loops #define FOR_3(i, a, b) for (int i = (a), bb##i = (b); i <= bb##i; ++i) #define FORR_3(i, a, b) for (int i = (b), aa##i = (a); i >= aa##i; --i) #define FOR_2(i, n) FOR_3(i, 0, (n)-1) #define FORR_2(i, n) FORR_3(i, 0, (n)-1) #define FOR_1(n) FOR_2(_i##__LINE__, n) #define FOR_0() for (;;) #define OVERLOAD(_0, _1, _2, _3, NAME, ...) NAME #define FOR(...) \ OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FOR_3, FOR_2, FOR_1, FOR_0) \ (__VA_ARGS__) #define FORR(...) \ OVERLOAD(_0 __VA_OPT__(, )##__VA_ARGS__, FORR_3, FORR_2, FORR_1, FOR_0) \ (__VA_ARGS__) // in - expr style template <class T> T read() { T x; cin >> x; return x; } #define II read<int>() #define ILL read<ll>() #define ISTR read<string>() // in - macro style void _I() {} template <class ARG, class... ARGS> void _I(ARG &&arg, ARGS &&...args) { cin >> arg; _I(forward<ARGS>(args)...); } #define I(...) _I(__VA_ARGS__); // out template <class T> void _print(T x, ostream &os = cout) { os << x; bool space = true; if constexpr (is_same_v<T, char>) if (x == '\n') space = false; if (space) os << " "; } // template <class T> void _print(const vector<T> &d, ostream &os = cout) { // os << '['; // for (auto &e : d) // os << e << " "; // os << ']'; // } void _O() {} template <class ARG, class... ARGS> void _O(ARG &&arg, ARGS &&...args) { _print(arg); _O(forward<ARGS>(args)...); } #define O(...) _O(__VA_ARGS__ __VA_OPT__(, ) '\n'); #define OO(...) _O(__VA_ARGS__); // err const int _indentSize = 2; string _indent; void _E() { cerr << endl; } template <class ARG, class... ARGS> void _E(ARG &&arg, ARGS &&...args) { _print(forward<ARG>(arg), cerr); _E(std::forward<ARGS>(args)...); } #define ERR(...) \ { \ cerr << _indent << "[" << __LINE__ << "] "; \ _E(__VA_ARGS__); \ } #define _V5(x) #x, "==", (x) #define _V4(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V5(__VA_ARGS__)) #define _V3(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V4(__VA_ARGS__)) #define _V2(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V3(__VA_ARGS__)) #define _V1(x, ...) #x, "==", (x)__VA_OPT__(, "|", _V2(__VA_ARGS__)) #define ERRV(...) ERR(_V1(__VA_ARGS__)) #define INDENT auto _indenter = Indenter(); struct Indenter { Indenter() { FOR(_indentSize) _indent.push_back(' '); } ~Indenter() { FOR(_indentSize) _indent.pop_back(); } }; #ifdef DEBUG #define FAIL(str) \ { \ cerr << "ASSERT FAILED " << str << " " << __FILE__ << ':' << __LINE__ \ << endl; \ abort(); \ } #else #define FAIL(x) \ {} #endif // asserts #define CHECK(cond) \ if (!(cond)) \ FAIL(#cond) #define CHECK_OP(op, a, b, res) \ if (!(res)) \ FAIL(#a " " op " " #b " (" + to_string(a) + " vs " + to_string(b) + ")") #define CHECK_EQ(a, b) CHECK_OP("==", a, b, (a) == (b)) #define CHECK_NE(a, b) CHECK_OP("!=", a, b, (a) != (b)) #define CHECK_LE(a, b) CHECK_OP("<=", a, b, (a) <= (b)) #define CHECK_GE(a, b) CHECK_OP(">=", a, b, (a) >= (b)) #define CHECK_LT(a, b) CHECK_OP("<", a, b, (a) < (b)) #define CHECK_GT(a, b) CHECK_OP(">", a, b, (a) > (b)) #define CHECK_RANGE(x, a, b) \ if (!(a <= x && x < b)) \ FAIL(#x " in [" #a "," #b ")") // #define SZ(v) ((int)(v).size()) using pii = pair<int, int>; // template <class T, class E> T fpow(T a, E exp) { T r = 1; while (exp) { if (exp & 1) { r *= a; --exp; } else { exp >>= 1; a *= a; } } return r; } //////////////////////////////////////////////////////////////////////////////// template <int MOD> class Modulo { public: using H = int; H h = 0; public: Modulo() = default; Modulo(H x) : h((x + MOD) % MOD){CHECK_GE(x + MOD, 0)} Modulo(const Modulo &) = default; public: Modulo &operator+=(const Modulo &o) { h += o.h; if (h >= MOD) h -= MOD; return *this; } Modulo &operator-=(const Modulo &o) { h = MOD + h - o.h; if (h >= MOD) h -= MOD; return *this; } Modulo &operator*=(const Modulo &o) { h = (1ULL * h * o.h) % MOD; return *this; } Modulo &operator/=(const Modulo &o) { const auto TOTIENT = MOD - 1; *this *= fpow(o, TOTIENT - 1); return *this; } bool operator==(const Modulo &o) const { return h == o.h; } bool operator!=(const Modulo &o) const { return h != o.h; } Modulo operator+(const Modulo &o) const { return Modulo(*this) += o; } Modulo operator-(const Modulo &o) const { return Modulo(*this) -= o; } Modulo operator*(const Modulo &o) const { return Modulo(*this) *= o; } Modulo operator/(const Modulo &o) const { return Modulo(*this) /= o; } Modulo &operator--() { if (h == 0) h = MOD; --h; return *this; } Modulo &operator++() { ++h; if (h == MOD) h = 0; return *this; } Modulo operator--(int) { auto m = *this; --(*this); return m; } Modulo operator++(int) { auto m = *this; ++(*this); return m; } explicit operator H() const { return h; } }; // snippets/modulo.hpp //////////////////////////////////////////////////////////////////////////////// // // snippets/binomial.hpp // template <class VAL> struct Binomial { int n = 0; int k = 0; VAL val = (VAL)1; public: Binomial() = default; Binomial(int nn, int kk) { (*this)(nn, kk); } // hack Binomial(int nn, int kk, const VAL &vv) : n(nn), k(kk), val(vv) {} operator const VAL &() const { return val; } VAL operator()(int nn, int kk) { if (outside(nn, kk)) return VAL(0); while (k > kk) move_l(); while (n > nn) move_u(); while (n < nn) move_d(); while (k < kk) move_r(); return val; } static bool outside(int nn, int kk) { return nn < 0 || kk < 0 || kk > nn; } void move_l() { --k; val *= VAL(k + 1); val /= VAL(n - k); } void move_r() { ++k; val *= VAL(n - k + 1); val /= VAL(k); } void move_d() { ++n; val *= VAL(n); val /= VAL(n - k); } void move_u() { --n; val *= VAL(n - k + 1); val /= VAL(n + 1); } }; using F = Modulo<int(1e9) + 7>; // using F = Modulo<998'244'353>; ostream &operator<<(ostream &os, const F &modulo) { return os << modulo.h; } using B = Binomial<F>; //// using ll = long long; // struct In { int n, m; } in; struct V { int bit; int parity = -1; vector<int> edges; }; vector<V> vs; vector<char> vis; struct Data { int num = 0; int num_per_parity[2] = {}; int num_bit[2] = {}; int num_bit_per_parity[2][2] = {}; int num_edges = 0; int odd = false; bool have_move = false; }; Data dfs_data; void dfs(int v, int parity) { if (vs[v].parity == -1) { vs[v].parity = parity; } else { if (vs[v].parity != parity) { dfs_data.odd = true; } } if (vis[v]) return; vis[v] = 1; dfs_data.num++; dfs_data.num_per_parity[parity]++; dfs_data.num_bit[vs[v].bit]++; dfs_data.num_bit_per_parity[parity][vs[v].bit]++; for (int u : vs[v].edges) { dfs_data.num_edges++; if (vs[v].bit == vs[u].bit) dfs_data.have_move = true; dfs(u, parity ^ 1); } } void read_input() { in.n = II; in.m = II; FOR(in.n) { int bit = II; vs.emplace_back(); vs.back().bit = bit; } FOR(in.m) { int ai = II - 1; int bi = II - 1; vs[ai].edges.push_back(bi); vs[bi].edges.push_back(ai); } } void generate_input() { in.n = 200'000; in.m = 0; FOR(in.n) { int bit = rand()%2; vs.emplace_back(); vs.back().bit = bit; } } int main() { ios::sync_with_stdio(0); cin.tie(0); read_input(); // generate_input(); vis.resize(in.n); F result = (F)1; FOR(v, in.n) { dfs_data = Data{}; dfs(v, 0); dfs_data.num_edges /= 2; if (dfs_data.num == 0) continue; // ERRV(v + 1, dfs_data.odd, dfs_data.num); if (dfs_data.odd) { if (dfs_data.have_move) { F cand = 0; B binomial; auto mod2 = dfs_data.num_bit[1] % 2; FOR(num1, 0, dfs_data.num) { if (num1 % 2 == mod2) cand += binomial(dfs_data.num, num1); } result *= cand; } } else { // INDENT; auto diff = dfs_data.num_bit_per_parity[1][1] - dfs_data.num_bit_per_parity[0][1]; auto binomial0 = B(); auto binomial1 = B(); F cand = (F)0; FOR(num1_parity0, 0, dfs_data.num_per_parity[0]) { // INDENT; auto num1_parity1 = num1_parity0 + diff; if (num1_parity1 > dfs_data.num_per_parity[1]) break; F part = binomial0(dfs_data.num_per_parity[0], num1_parity0) * binomial1(dfs_data.num_per_parity[1], num1_parity1); // ERRV(part); cand += part; } // ERRV(cand); result *= cand; } } O(result); return 0; } |