//GRT_2018 #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define all(x) x.begin(), x.end() //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int mod = 1e9 + 7, nax = 200 * 1000 + 10; int n, m; int c[nax]; int fact[nax], inv[nax]; bool col[nax]; int bi(int a, int b) { ll me = ((ll)fact[a] * inv[b]) % mod; me = (me * inv[a - b]) % mod; return me; } int fastpow(int a, int b) { int w = 1; while (b) { if (b & 1) w = ((ll)w * a) % mod; b >>= 1; a = ((ll)a * a) % mod; } return w; } vi V[nax]; bool vis[nax]; int o1, o2, sz1, sz2; bool odd; void dfs(int x) { vis[x] = true; if (col[x]) { o1 += (c[x] == 1); sz1++; } else { o2 += (c[x] == 1); sz2++; } for (int y : V[x]) { if (!vis[y]) { col[y] = 1 - col[x]; dfs(y); } else if (col[y] == col[x]) odd = true; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; fact[0] = 1; inv[0] = 1; for (int i = 1; i <= n; ++i) { fact[i] = ((ll)i * fact[i-1]) % mod; inv[i] = fastpow(fact[i], mod - 2); } for (int i = 1; i <= n; ++i) cin >> c[i]; for (int a,b, i = 0; i < m; ++i) { cin >> a >> b; V[a].PB(b); V[b].PB(a); } int ans = 1; for (int i = 1; i <= n; ++i) { if (!vis[i]) { int ret = 0; o1 = o2 = sz1 = sz2 = 0; odd = false; dfs(i); // cerr << o1 << " " << o2 << " " << sz1 << " " << sz2 << " " << odd << "\n"; if (odd) { int good = (o1 + o2) & 1; for (int s = 0; s <= sz1 + sz2; ++s) { if (s % 2 == good) { // cerr << sz1 + sz2 << " choose " << s << "\n"; ret = (ret + bi(sz1 + sz2, s)) % mod; } } } else { int diff = o2 - o1; for (int s = 0; s <= sz1; ++s) { int he = s + diff; if (he < 0 || he > sz2) continue; ret = (ret + (ll)bi(sz1, s) * bi(sz2, he)) % mod; } } ans = ((ll)ans * ret) % mod; // cerr << ret << "\n"; } } cout << ans; }
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 | //GRT_2018 #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define all(x) x.begin(), x.end() //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int mod = 1e9 + 7, nax = 200 * 1000 + 10; int n, m; int c[nax]; int fact[nax], inv[nax]; bool col[nax]; int bi(int a, int b) { ll me = ((ll)fact[a] * inv[b]) % mod; me = (me * inv[a - b]) % mod; return me; } int fastpow(int a, int b) { int w = 1; while (b) { if (b & 1) w = ((ll)w * a) % mod; b >>= 1; a = ((ll)a * a) % mod; } return w; } vi V[nax]; bool vis[nax]; int o1, o2, sz1, sz2; bool odd; void dfs(int x) { vis[x] = true; if (col[x]) { o1 += (c[x] == 1); sz1++; } else { o2 += (c[x] == 1); sz2++; } for (int y : V[x]) { if (!vis[y]) { col[y] = 1 - col[x]; dfs(y); } else if (col[y] == col[x]) odd = true; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; fact[0] = 1; inv[0] = 1; for (int i = 1; i <= n; ++i) { fact[i] = ((ll)i * fact[i-1]) % mod; inv[i] = fastpow(fact[i], mod - 2); } for (int i = 1; i <= n; ++i) cin >> c[i]; for (int a,b, i = 0; i < m; ++i) { cin >> a >> b; V[a].PB(b); V[b].PB(a); } int ans = 1; for (int i = 1; i <= n; ++i) { if (!vis[i]) { int ret = 0; o1 = o2 = sz1 = sz2 = 0; odd = false; dfs(i); // cerr << o1 << " " << o2 << " " << sz1 << " " << sz2 << " " << odd << "\n"; if (odd) { int good = (o1 + o2) & 1; for (int s = 0; s <= sz1 + sz2; ++s) { if (s % 2 == good) { // cerr << sz1 + sz2 << " choose " << s << "\n"; ret = (ret + bi(sz1 + sz2, s)) % mod; } } } else { int diff = o2 - o1; for (int s = 0; s <= sz1; ++s) { int he = s + diff; if (he < 0 || he > sz2) continue; ret = (ret + (ll)bi(sz1, s) * bi(sz2, he)) % mod; } } ans = ((ll)ans * ret) % mod; // cerr << ret << "\n"; } } cout << ans; } |