#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,auto x)->decltype(x.end(),o); auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";} 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 = int(1e9) + 7; 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); } 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) { return mul(fac[n], mul(rev[n - k], rev[k])); } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector<int> in(n); REP(i, n) cin >> in[i]; vector<vector<int>> graph(n); REP(i, m) { int a, b; cin >> a >> b; --a, --b; graph[a].emplace_back(b); graph[b].emplace_back(a); } debug(n, m, in, graph); BinomCoeff bc(n); int ans = 1; vector<int> color(n, -1); vector<bool> visited(n); REP(i, n) { if (visited[i]) continue; vector cnt(2, 0), cnt_ones(2, 0); bool odd_cycle = false; function<void(int, int)> dfs = [&](int v, int c) { visited[v] = true; color[v] = c; ++cnt[c]; cnt_ones[c] += in[v]; for (auto x : graph[v]) { if (visited[x]) { if (color[x] == c) odd_cycle = true; } else { dfs(x, c ^ 1); } } }; dfs(i, 0); debug(i, odd_cycle, cnt, cnt_ones); if (odd_cycle) { ans = mul(ans, powi(2, cnt[0] + cnt[1] - 1)); } else { const int size_left = cnt[0]; const int size_right = cnt[1]; int one_left = cnt_ones[0]; int one_right = cnt_ones[1]; int zero_left = cnt[0] - cnt_ones[0]; int zero_right = cnt[1] - cnt_ones[1]; { int diff = min(one_left, one_right); one_left -= diff; one_right -= diff; zero_left += diff; zero_right += diff; } int sum = 0; while (true) { sum = add(sum, mul(bc(size_left, one_left), bc(size_right, one_right))); if (not zero_left or not zero_right) { break; } ++one_left; ++one_right; --zero_left; --zero_right; } ans = mul(ans, sum); } } 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 | #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,auto x)->decltype(x.end(),o); auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";} 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 = int(1e9) + 7; 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); } 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) { return mul(fac[n], mul(rev[n - k], rev[k])); } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector<int> in(n); REP(i, n) cin >> in[i]; vector<vector<int>> graph(n); REP(i, m) { int a, b; cin >> a >> b; --a, --b; graph[a].emplace_back(b); graph[b].emplace_back(a); } debug(n, m, in, graph); BinomCoeff bc(n); int ans = 1; vector<int> color(n, -1); vector<bool> visited(n); REP(i, n) { if (visited[i]) continue; vector cnt(2, 0), cnt_ones(2, 0); bool odd_cycle = false; function<void(int, int)> dfs = [&](int v, int c) { visited[v] = true; color[v] = c; ++cnt[c]; cnt_ones[c] += in[v]; for (auto x : graph[v]) { if (visited[x]) { if (color[x] == c) odd_cycle = true; } else { dfs(x, c ^ 1); } } }; dfs(i, 0); debug(i, odd_cycle, cnt, cnt_ones); if (odd_cycle) { ans = mul(ans, powi(2, cnt[0] + cnt[1] - 1)); } else { const int size_left = cnt[0]; const int size_right = cnt[1]; int one_left = cnt_ones[0]; int one_right = cnt_ones[1]; int zero_left = cnt[0] - cnt_ones[0]; int zero_right = cnt[1] - cnt_ones[1]; { int diff = min(one_left, one_right); one_left -= diff; one_right -= diff; zero_left += diff; zero_right += diff; } int sum = 0; while (true) { sum = add(sum, mul(bc(size_left, one_left), bc(size_right, one_right))); if (not zero_left or not zero_right) { break; } ++one_left; ++one_right; --zero_left; --zero_right; } ans = mul(ans, sum); } } cout << ans << '\n'; } |