#if defined(EMBE_DEBUG) && !defined(NDEBUG)
#include "embe-debug.hpp"
#else
#define LOG_INDENT(...) do {} while (false)
#define LOG(...) do {} while (false)
#define DUMP(...) do {} while (false)
#endif
#include <iostream>
#include <vector>
using namespace std;
namespace {
constexpr int modulo = 1'000'000'007;
int mul(int a, int b)
{
return static_cast<long long>(a) * b % modulo;
}
int power(int a, int b)
{
if (b == 0) return 1;
auto tmp = power(a, b / 2);
auto tmp2 = mul(tmp, tmp);
if (b % 2 == 0) {
return tmp2;
} else {
return mul(tmp2, a);
}
}
int div(int a, int b)
{
return mul(a, power(b, modulo - 2));
}
int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; ++i) {
res = mul(res, i);
}
return res;
}
int choose(int n, int k)
{
return div(fact(n), mul(fact(k), fact(n - k)));
}
void count_tokens(int v, int c, vector<int> const& colors, vector<vector<int>> const& neigh, vector<int>& state, int& cnt, bool& two_col, int& tokens)
{
if (state[v] >= 0) return;
++cnt;
state[v] = c;
tokens += colors[v] ^ c;
for (auto const& w: neigh[v]) {
int d = 1 - c;
if (state[w] < 0) {
count_tokens(w, d, colors, neigh, state, cnt, two_col, tokens);
} else if (state[w] != d) {
two_col = false;
}
}
}
int solve(vector<int> const& colors, vector<vector<int>> const& neigh)
{
int res = 1;
int n = colors.size();
vector<int> state(n, -1);
for (int i = 0; i < n; ++i) {
if (state[i] >= 0) continue;
int cnt = 0;
bool two_col = true;
int tokens = 0;
count_tokens(i, 0, colors, neigh, state, cnt, two_col, tokens);
int cur;
if (two_col) {
cur = choose(cnt, tokens);
} else {
cur = power(2, cnt - 1);
}
res = mul(res, cur);
}
return res;
}
}
int main()
{
iostream::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> colors(n);
for (auto& c: colors) {
cin >> c;
}
vector<vector<int>> neigh(n);
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
neigh[x - 1].push_back(y - 1);
neigh[y - 1].push_back(x - 1);
}
int res = solve(colors, neigh);
cout << res << endl;
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 | #if defined(EMBE_DEBUG) && !defined(NDEBUG) #include "embe-debug.hpp" #else #define LOG_INDENT(...) do {} while (false) #define LOG(...) do {} while (false) #define DUMP(...) do {} while (false) #endif #include <iostream> #include <vector> using namespace std; namespace { constexpr int modulo = 1'000'000'007; int mul(int a, int b) { return static_cast<long long>(a) * b % modulo; } int power(int a, int b) { if (b == 0) return 1; auto tmp = power(a, b / 2); auto tmp2 = mul(tmp, tmp); if (b % 2 == 0) { return tmp2; } else { return mul(tmp2, a); } } int div(int a, int b) { return mul(a, power(b, modulo - 2)); } int fact(int n) { int res = 1; for (int i = 2; i <= n; ++i) { res = mul(res, i); } return res; } int choose(int n, int k) { return div(fact(n), mul(fact(k), fact(n - k))); } void count_tokens(int v, int c, vector<int> const& colors, vector<vector<int>> const& neigh, vector<int>& state, int& cnt, bool& two_col, int& tokens) { if (state[v] >= 0) return; ++cnt; state[v] = c; tokens += colors[v] ^ c; for (auto const& w: neigh[v]) { int d = 1 - c; if (state[w] < 0) { count_tokens(w, d, colors, neigh, state, cnt, two_col, tokens); } else if (state[w] != d) { two_col = false; } } } int solve(vector<int> const& colors, vector<vector<int>> const& neigh) { int res = 1; int n = colors.size(); vector<int> state(n, -1); for (int i = 0; i < n; ++i) { if (state[i] >= 0) continue; int cnt = 0; bool two_col = true; int tokens = 0; count_tokens(i, 0, colors, neigh, state, cnt, two_col, tokens); int cur; if (two_col) { cur = choose(cnt, tokens); } else { cur = power(2, cnt - 1); } res = mul(res, cur); } return res; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> colors(n); for (auto& c: colors) { cin >> c; } vector<vector<int>> neigh(n); for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; neigh[x - 1].push_back(y - 1); neigh[y - 1].push_back(x - 1); } int res = solve(colors, neigh); cout << res << endl; return 0; } |
English