#include <bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;} auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";} #define debug(X) cerr << "["#X"]: " << X << '\n'; #else #define cerr if(0)cout #define debug(X) ; #endif using ll = long long; #define all(v) (v).begin(), (v).end() #define ssize(x) int(x.size()) #define fi first #define se second #define mp make_pair #define eb emplace_back const int mod = 1'000'000'007; void inc(int &a, int b) { a += b; if(a >= mod) a -= mod; } int mul(int a, int b) { return int(((ll)a*b)%mod); } int fpow(int a, int b) { if(b == 0) return 1; if(b%2 == 0) return fpow(mul(a, a), b/2); else return mul(a, fpow(a, b-1)); } int inv(int x) { return fpow(x, mod-2); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> c(n); for(int i = 0; i < n; ++i) { cin >> c[i]; } vector<vector<int>> g(n); for(int i = 0; i < m; ++i) { int a, b; cin >> a >> b; a--; b--; g[a].eb(b); g[b].eb(a); } vector<int> sil(n+1); sil[0] = 1; for(int i = 1; i <= n; ++i) sil[i] = mul(i, sil[i-1]); auto choose = [&](int a, int b) { return mul(sil[a], inv(mul(sil[a-b], sil[b]))); }; vector<int> col(n, -1); vector<int> sp; int a = 0, b = 0; //a = #0, b = #1 function<bool(int, int)> dfs = [&](int v, int color) { col[v] = color; sp.eb(v); if(color == 0) ++a; else ++b; bool ok = true; for(int u : g[v]) { if(col[u] == -1) ok &= dfs(u, 1-color); else ok &= (col[u] != col[v]); } return ok; }; int res = 1; for(int i = 0; i < n; ++i) { if(col[i] != -1) continue; sp.clear(); int old_a = a, old_b = b; bool dwu = dfs(i, 0); // debug(sp); // debug(dwu); if(!dwu) { int sz = a+b-(old_a+old_b); res = mul(res, fpow(2, sz-1)); } else { int da = a - old_a, db = b-old_b; // debug(da); // debug(db); int x = 0, y = 0; for(int v : sp) { if(c[v] == 0) continue; if(col[v] == 0) ++x; else ++y; } // debug(x); // debug(y); assert(x <= da && y <= db); int podz = 0; if(x >= y) { // debug(x-y); for(int k = x-y; k <= da && k-(x-y) <= db; ++k) { // debug(k); assert(k >= 0 && k <= da); assert(k-(x-y) >= 0); assert(k-(x-y) <= db); inc(podz, mul(choose(da, k), choose(db, k-(x-y)))); } } else { debug(y-x); for(int k = y-x; k <= db && k-(y-x) <= da; ++k) { // debug(k); assert(k >= 0 && k <= db); assert(k-(y-x) >= 0 && k-(y-x) <= da); inc(podz, mul(choose(da, k-(y-x)), choose(db, k))); } } // debug(podz); res = mul(res, podz); //res *= ile jest podzialow tak ze liczba 1 w da - liczba 1 w db == x-y } // debug(res); // cerr << endl; } cout << res << '\n'; 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 | #include <bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;} auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";} #define debug(X) cerr << "["#X"]: " << X << '\n'; #else #define cerr if(0)cout #define debug(X) ; #endif using ll = long long; #define all(v) (v).begin(), (v).end() #define ssize(x) int(x.size()) #define fi first #define se second #define mp make_pair #define eb emplace_back const int mod = 1'000'000'007; void inc(int &a, int b) { a += b; if(a >= mod) a -= mod; } int mul(int a, int b) { return int(((ll)a*b)%mod); } int fpow(int a, int b) { if(b == 0) return 1; if(b%2 == 0) return fpow(mul(a, a), b/2); else return mul(a, fpow(a, b-1)); } int inv(int x) { return fpow(x, mod-2); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> c(n); for(int i = 0; i < n; ++i) { cin >> c[i]; } vector<vector<int>> g(n); for(int i = 0; i < m; ++i) { int a, b; cin >> a >> b; a--; b--; g[a].eb(b); g[b].eb(a); } vector<int> sil(n+1); sil[0] = 1; for(int i = 1; i <= n; ++i) sil[i] = mul(i, sil[i-1]); auto choose = [&](int a, int b) { return mul(sil[a], inv(mul(sil[a-b], sil[b]))); }; vector<int> col(n, -1); vector<int> sp; int a = 0, b = 0; //a = #0, b = #1 function<bool(int, int)> dfs = [&](int v, int color) { col[v] = color; sp.eb(v); if(color == 0) ++a; else ++b; bool ok = true; for(int u : g[v]) { if(col[u] == -1) ok &= dfs(u, 1-color); else ok &= (col[u] != col[v]); } return ok; }; int res = 1; for(int i = 0; i < n; ++i) { if(col[i] != -1) continue; sp.clear(); int old_a = a, old_b = b; bool dwu = dfs(i, 0); // debug(sp); // debug(dwu); if(!dwu) { int sz = a+b-(old_a+old_b); res = mul(res, fpow(2, sz-1)); } else { int da = a - old_a, db = b-old_b; // debug(da); // debug(db); int x = 0, y = 0; for(int v : sp) { if(c[v] == 0) continue; if(col[v] == 0) ++x; else ++y; } // debug(x); // debug(y); assert(x <= da && y <= db); int podz = 0; if(x >= y) { // debug(x-y); for(int k = x-y; k <= da && k-(x-y) <= db; ++k) { // debug(k); assert(k >= 0 && k <= da); assert(k-(x-y) >= 0); assert(k-(x-y) <= db); inc(podz, mul(choose(da, k), choose(db, k-(x-y)))); } } else { debug(y-x); for(int k = y-x; k <= db && k-(y-x) <= da; ++k) { // debug(k); assert(k >= 0 && k <= db); assert(k-(y-x) >= 0 && k-(y-x) <= da); inc(podz, mul(choose(da, k-(y-x)), choose(db, k))); } } // debug(podz); res = mul(res, podz); //res *= ile jest podzialow tak ze liczba 1 w da - liczba 1 w db == x-y } // debug(res); // cerr << endl; } cout << res << '\n'; return 0; } |