#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) 42 #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) #define each(a,x) for(auto& a : (x)) #define sz(x) int((x).size()) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } const int P = 1e9 + 7, phiP = P - 1; struct modint { int x = 0; modint operator+(modint o) const {return {x + o.x >= P ? x + o.x - P : x + o.x}; } modint operator-(modint o) const {return {x < o.x ? x - o.x + P : x - o.x}; } modint operator+() const {return *this; } modint operator-() const {return {x ? P - x : 0}; } modint operator*(modint o) const {return {int(ll(x) * o.x % P)}; } modint operator/(modint o) const {return *this * o.inv(); } modint & operator+=(modint o) {return *this = *this + o; } modint & operator-=(modint o) {return *this = *this - o; } modint & operator*=(modint o) {return *this = *this * o; } modint & operator/=(modint o) {return *this = *this / o; } modint pow(ll e) const { modint ret{1}, b(*this); for (; e; e >>= 1) { if (e & 1) ret *= b; b *= b; } return ret; } modint inv() const {return pow(phiP - 1); } friend void __dbg(modint m) { cout << m.x - P * (2 * m.x >= P); } }; vector<modint> fact = {modint{1}}, ifact = {modint{1}}; vvi g; vi on, vis; void dfs(int v, int col, array<array<int, 2>, 2> &counts, bool &twocol) { if (vis[v] == (col ^ 1)) twocol = false; if (vis[v] != -1) return; vis[v] = col; ++counts[col][on[v]]; for (int j : g[v]) dfs(j, col ^ 1, counts, twocol); } void solve() { rep(_, 3e5) { fact.push_back(fact.back() * modint{sz(fact)}); ifact.push_back(fact.back().inv()); } int n, m; cin >> n >> m; on.resize(n); vis.resize(n, -1); g.resize(n); rep(i, n) cin >> on[i]; rep(_, m) { int a, b; cin >> a >> b; --a; --b; g[a].push_back(b); g[b].push_back(a); } modint ans{1}; rep(i, n) { if (vis[i] != -1) continue; array<array<int, 2>, 2> counts{}; // [kolor][czy on] bool twocol = true; dfs(i, 0, counts, twocol); debug(i, twocol, counts); int ile = counts[0][0] + counts[0][1] + counts[1][0] + counts[1][1]; if (!twocol) { ans *= modint{2}.pow(ile - 1); continue; } modint here; for (int add = -ile; add <= ile; ++add) { if (add > counts[0][0]) continue; if (add > counts[1][0]) continue; if (add + counts[0][1] < 0) continue; if (add + counts[1][1] < 0) continue; here += fact[counts[0][0] + counts[0][1]] * ifact[counts[0][0] - add] * ifact[counts[0][1] + add] * fact[counts[1][0] + counts[1][1]] * ifact[counts[1][0] - add] * ifact[counts[1][1] + add]; } ans *= here; } cout << ans.x << '\n'; } int main() { #ifdef DEBUG const int MEMSIZE = 1024 * 1024 * 1024; static_assert(MEMSIZE % 16 == 0); static char stack[MEMSIZE]; asm volatile ( "mov %[newstack], %%rsp\n" "call *%[funcptr]" :: [funcptr] "r" (solve), [newstack] "r" (stack + MEMSIZE) ); exit(0); #else ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); #endif }
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) 42 #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) #define each(a,x) for(auto& a : (x)) #define sz(x) int((x).size()) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } const int P = 1e9 + 7, phiP = P - 1; struct modint { int x = 0; modint operator+(modint o) const {return {x + o.x >= P ? x + o.x - P : x + o.x}; } modint operator-(modint o) const {return {x < o.x ? x - o.x + P : x - o.x}; } modint operator+() const {return *this; } modint operator-() const {return {x ? P - x : 0}; } modint operator*(modint o) const {return {int(ll(x) * o.x % P)}; } modint operator/(modint o) const {return *this * o.inv(); } modint & operator+=(modint o) {return *this = *this + o; } modint & operator-=(modint o) {return *this = *this - o; } modint & operator*=(modint o) {return *this = *this * o; } modint & operator/=(modint o) {return *this = *this / o; } modint pow(ll e) const { modint ret{1}, b(*this); for (; e; e >>= 1) { if (e & 1) ret *= b; b *= b; } return ret; } modint inv() const {return pow(phiP - 1); } friend void __dbg(modint m) { cout << m.x - P * (2 * m.x >= P); } }; vector<modint> fact = {modint{1}}, ifact = {modint{1}}; vvi g; vi on, vis; void dfs(int v, int col, array<array<int, 2>, 2> &counts, bool &twocol) { if (vis[v] == (col ^ 1)) twocol = false; if (vis[v] != -1) return; vis[v] = col; ++counts[col][on[v]]; for (int j : g[v]) dfs(j, col ^ 1, counts, twocol); } void solve() { rep(_, 3e5) { fact.push_back(fact.back() * modint{sz(fact)}); ifact.push_back(fact.back().inv()); } int n, m; cin >> n >> m; on.resize(n); vis.resize(n, -1); g.resize(n); rep(i, n) cin >> on[i]; rep(_, m) { int a, b; cin >> a >> b; --a; --b; g[a].push_back(b); g[b].push_back(a); } modint ans{1}; rep(i, n) { if (vis[i] != -1) continue; array<array<int, 2>, 2> counts{}; // [kolor][czy on] bool twocol = true; dfs(i, 0, counts, twocol); debug(i, twocol, counts); int ile = counts[0][0] + counts[0][1] + counts[1][0] + counts[1][1]; if (!twocol) { ans *= modint{2}.pow(ile - 1); continue; } modint here; for (int add = -ile; add <= ile; ++add) { if (add > counts[0][0]) continue; if (add > counts[1][0]) continue; if (add + counts[0][1] < 0) continue; if (add + counts[1][1] < 0) continue; here += fact[counts[0][0] + counts[0][1]] * ifact[counts[0][0] - add] * ifact[counts[0][1] + add] * fact[counts[1][0] + counts[1][1]] * ifact[counts[1][0] - add] * ifact[counts[1][1] + add]; } ans *= here; } cout << ans.x << '\n'; } int main() { #ifdef DEBUG const int MEMSIZE = 1024 * 1024 * 1024; static_assert(MEMSIZE % 16 == 0); static char stack[MEMSIZE]; asm volatile ( "mov %[newstack], %%rsp\n" "call *%[funcptr]" :: [funcptr] "r" (solve), [newstack] "r" (stack + MEMSIZE) ); exit(0); #else ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); #endif } |