// // Mateusz Pietrowcow // #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define MODLL 1000000007LL #define MODULL 1000000007ULL #define INF 1000000000 #define INFL 1000000000000000000LL #define Tak cout << "TAK" << '\n' #define Nie cout << "NIE" << '\n' #define min3(x, y, z) min(x, min(y, z)) #define max3(x, y, z) max(x, max(y, z)) #define all(x) x.begin(),x.end() using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int,int> ii; typedef pair<long long, long long> pll; typedef pair<unsigned long long, unsigned long long> pull; const int N = 2e5 + 5; vector<int> g[N]; bitset<N> t, visited; ull silnia[N]; ll pref[N]; int type[N]; int n, m, k, p, np, lp, lnp; bool cp, cnp; inline ll mod(ll a) { return ((a % MODULL) + MODLL) % MODLL; } inline ull mod(ull a) { return a % MODULL; } ull fastPot(ull a, ull b) { ull r = 1; while (b != 0) { if (b % 2 == 1) r = mod(r * a); a = mod(a * a); b /= 2; } return r; } ull newton(ull n, ull k) { ull r = silnia[n], pot = fastPot(mod(silnia[k] * silnia[n - k]), MODULL - 2); return mod(r * pot); } void init() { silnia[0] = 1; for (ull i = 1; i <= n; i++) silnia[i] = mod((ull)silnia[i - 1] * i); } int mod2(int a) { return ((a % 2) + 2) % 2; } void read() { cin >> n >> m; for (int i = 1; i <= n; i++) { bool c; cin >> c; t[i] = c; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } } void dfs(int v, int par) { visited[v] = true; type[v] = !type[par]; if (type[v]) { lnp++; np += t[v]; } else { lp++; p += t[v]; } k++; for (auto i : g[v]) { if (visited[i]) { bool r1 = type[i], r2 = type[v]; if (r1 == r2) { if (!r1) cp = true; else cnp = true; } } else dfs(i, v); } } void solve() { init(); ull res = 1; for (int i = 1; i <= n; i++) { if (visited[i]) continue; k = p = np = lp = lnp = cp = cnp = 0; type[i] = true; dfs(i, i); if (k == 1) continue; ull r = 0; if (!cp && !cnp) { int z = min(np, p); p = (p - z); np = (np - z); for (int i = 0; i <= min(lnp - np, lp - p); i++) r = mod(r + mod(newton(lnp, np + i) * newton(lp, p + i))); } else { int z = min(np, p); p = (p - z) % 2; np = (np - z) % 2; pref[0] = pref[1] = 0; for (int i = 2; i <= lnp + 2; i += 2) pref[i] = mod(pref[i - 2] + newton(lnp, i - 2)); for (int i = 3; i <= lnp + 2; i += 2) pref[i] = mod(pref[i - 2] + newton(lnp, i - 2)); for (int i = 0; i <= lp; i++) { int start = (mod2(np + i - p)), end = lnp - ((mod2(np + i - p)) ^ (lnp % 2)); if (start > lp) continue; r += mod(newton(lp, i) * mod(pref[end + 2] - pref[start])); r = mod(r); } } res = mod(res * r); } cout << res; } int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); read(); solve(); }
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | // // Mateusz Pietrowcow // #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define MODLL 1000000007LL #define MODULL 1000000007ULL #define INF 1000000000 #define INFL 1000000000000000000LL #define Tak cout << "TAK" << '\n' #define Nie cout << "NIE" << '\n' #define min3(x, y, z) min(x, min(y, z)) #define max3(x, y, z) max(x, max(y, z)) #define all(x) x.begin(),x.end() using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int,int> ii; typedef pair<long long, long long> pll; typedef pair<unsigned long long, unsigned long long> pull; const int N = 2e5 + 5; vector<int> g[N]; bitset<N> t, visited; ull silnia[N]; ll pref[N]; int type[N]; int n, m, k, p, np, lp, lnp; bool cp, cnp; inline ll mod(ll a) { return ((a % MODULL) + MODLL) % MODLL; } inline ull mod(ull a) { return a % MODULL; } ull fastPot(ull a, ull b) { ull r = 1; while (b != 0) { if (b % 2 == 1) r = mod(r * a); a = mod(a * a); b /= 2; } return r; } ull newton(ull n, ull k) { ull r = silnia[n], pot = fastPot(mod(silnia[k] * silnia[n - k]), MODULL - 2); return mod(r * pot); } void init() { silnia[0] = 1; for (ull i = 1; i <= n; i++) silnia[i] = mod((ull)silnia[i - 1] * i); } int mod2(int a) { return ((a % 2) + 2) % 2; } void read() { cin >> n >> m; for (int i = 1; i <= n; i++) { bool c; cin >> c; t[i] = c; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } } void dfs(int v, int par) { visited[v] = true; type[v] = !type[par]; if (type[v]) { lnp++; np += t[v]; } else { lp++; p += t[v]; } k++; for (auto i : g[v]) { if (visited[i]) { bool r1 = type[i], r2 = type[v]; if (r1 == r2) { if (!r1) cp = true; else cnp = true; } } else dfs(i, v); } } void solve() { init(); ull res = 1; for (int i = 1; i <= n; i++) { if (visited[i]) continue; k = p = np = lp = lnp = cp = cnp = 0; type[i] = true; dfs(i, i); if (k == 1) continue; ull r = 0; if (!cp && !cnp) { int z = min(np, p); p = (p - z); np = (np - z); for (int i = 0; i <= min(lnp - np, lp - p); i++) r = mod(r + mod(newton(lnp, np + i) * newton(lp, p + i))); } else { int z = min(np, p); p = (p - z) % 2; np = (np - z) % 2; pref[0] = pref[1] = 0; for (int i = 2; i <= lnp + 2; i += 2) pref[i] = mod(pref[i - 2] + newton(lnp, i - 2)); for (int i = 3; i <= lnp + 2; i += 2) pref[i] = mod(pref[i - 2] + newton(lnp, i - 2)); for (int i = 0; i <= lp; i++) { int start = (mod2(np + i - p)), end = lnp - ((mod2(np + i - p)) ^ (lnp % 2)); if (start > lp) continue; r += mod(newton(lp, i) * mod(pref[end + 2] - pref[start])); r = mod(r); } } res = mod(res * r); } cout << res; } int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); read(); solve(); } |