#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1e9 + 7;
int n, m;
vector<bool> d;
vector<int> v[200009];
int odw[200009];
LL xGCD(LL a, LL b, LL &x, LL &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
LL x1, y1, gcd = xGCD(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return gcd;
}
LL modfact(LL n) {
LL result = 1;
while (n > 1) {
result = result * n % MOD;
n -= 1;
}
return result;
}
LL modmult(LL a, LL b) {
return a * b % MOD;
}
LL inverse(LL a) {
LL x, y;
xGCD(a, MOD, x, y);
return x;
}
LL bc(LL n, LL k) {
return modmult(modmult(modfact(n), inverse(modfact(k))), inverse(modfact(n - k)));
}
int zlicz = 0;
int pa[2], ni[2];
pair<int, int> dfs(int st, int x) {
odw[st] = x;
zlicz++;
if(d[st]){
pa[x%2]++;
}else{
ni[x%2]++;
}
pair<int, int> res = {zlicz, 0};
for (auto q : v[st]) {
if (odw[q] == 0) {
auto [a, b] = dfs(q, x + 1);
res.first = max(res.first, a);
res.second = max(res.second, b);
} else {
if ((x + odw[q]) % 2 == 0) {
res.second = 1;
}
}
}
return res;
}
LL solve() {
LL res = 1;
for (int i = 0; i < n; i++) {
if (odw[i] == 0) {
zlicz = 0;
for(int j=0; j<2; j++){
pa[j] = 0;
ni[j] = 0 ;
}
auto [a, b] = dfs(i, 1);
if (b == 1) {
LL ans = 1;
for (int i = 1; i < a; i++) {
ans = (ans * 2) % MOD;
}
res = (res * ans) % MOD;
} else {
int ile = min(pa[0] + ni[1], pa[1] + ni[0]);
res = (res * (bc(a, ile) + MOD)) % MOD;
}
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < n; i++) {
bool a;
cin >> a;
d.push_back(a);
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
v[a].push_back(b);
v[b].push_back(a);
}
cout << solve() << "\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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; const LL MOD = 1e9 + 7; int n, m; vector<bool> d; vector<int> v[200009]; int odw[200009]; LL xGCD(LL a, LL b, LL &x, LL &y) { if (b == 0) { x = 1; y = 0; return a; } LL x1, y1, gcd = xGCD(b, a % b, x1, y1); x = y1; y = x1 - (a / b) * y1; return gcd; } LL modfact(LL n) { LL result = 1; while (n > 1) { result = result * n % MOD; n -= 1; } return result; } LL modmult(LL a, LL b) { return a * b % MOD; } LL inverse(LL a) { LL x, y; xGCD(a, MOD, x, y); return x; } LL bc(LL n, LL k) { return modmult(modmult(modfact(n), inverse(modfact(k))), inverse(modfact(n - k))); } int zlicz = 0; int pa[2], ni[2]; pair<int, int> dfs(int st, int x) { odw[st] = x; zlicz++; if(d[st]){ pa[x%2]++; }else{ ni[x%2]++; } pair<int, int> res = {zlicz, 0}; for (auto q : v[st]) { if (odw[q] == 0) { auto [a, b] = dfs(q, x + 1); res.first = max(res.first, a); res.second = max(res.second, b); } else { if ((x + odw[q]) % 2 == 0) { res.second = 1; } } } return res; } LL solve() { LL res = 1; for (int i = 0; i < n; i++) { if (odw[i] == 0) { zlicz = 0; for(int j=0; j<2; j++){ pa[j] = 0; ni[j] = 0 ; } auto [a, b] = dfs(i, 1); if (b == 1) { LL ans = 1; for (int i = 1; i < a; i++) { ans = (ans * 2) % MOD; } res = (res * ans) % MOD; } else { int ile = min(pa[0] + ni[1], pa[1] + ni[0]); res = (res * (bc(a, ile) + MOD)) % MOD; } } } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (int i = 0; i < n; i++) { bool a; cin >> a; d.push_back(a); } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; v[a].push_back(b); v[b].push_back(a); } cout << solve() << "\n"; return 0; } |
English