#include <bits/stdc++.h> using namespace std; template <typename T> T mod_inv_in_range(T a, T m) { // assert(0 <= a && a < m); T x = a, y = m; // coeff of a in x and y T vx = 1, vy = 0; while (x) { T k = y / x; y %= x; vy -= k * vx; std::swap(x, y); std::swap(vx, vy); } assert(y == 1); return vy < 0 ? m + vy : vy; } template <typename T> struct extended_gcd_result { T gcd; T coeff_a, coeff_b; }; template <typename T> extended_gcd_result<T> extended_gcd(T a, T b) { T x = a, y = b; // coeff of a and b in x and y T ax = 1, ay = 0; T bx = 0, by = 1; while (x) { T k = y / x; y %= x; ay -= k * ax; by -= k * bx; std::swap(x, y); std::swap(ax, ay); std::swap(bx, by); } return {y, ay, by}; } template <typename T> T mod_inv(T a, T m) { a %= m; a = a < 0 ? a + m : a; return mod_inv_in_range(a, m); } template <int MOD_> struct modnum { static constexpr int MOD = MOD_; static_assert(MOD_ > 0, "MOD must be positive"); private: int v; public: modnum() : v(0) {} modnum(int64_t v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); } friend std::istream& operator >> (std::istream& in, modnum& n) { int64_t v_; in >> v_; n = modnum(v_); return in; } friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; } friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = mod_inv_in_range(v, MOD); return res; } friend modnum inv(const modnum& m) { return m.inv(); } modnum neg() const { modnum res; res.v = v ? MOD-v : 0; return res; } friend modnum neg(const modnum& m) { return m.neg(); } modnum operator- () const { return neg(); } modnum operator+ () const { return modnum(*this); } modnum& operator ++ () { v ++; if (v == MOD) v = 0; return *this; } modnum& operator -- () { if (v == 0) v = MOD; v --; return *this; } modnum& operator += (const modnum& o) { v -= MOD-o.v; v = (v < 0) ? v + MOD : v; return *this; } modnum& operator -= (const modnum& o) { v -= o.v; v = (v < 0) ? v + MOD : v; return *this; } modnum& operator *= (const modnum& o) { v = int(int64_t(v) * int64_t(o.v) % MOD); return *this; } modnum& operator /= (const modnum& o) { return *this *= o.inv(); } friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; } friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; } friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; } friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; } friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; } friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; } }; template <typename T> T pow(T a, long long b) { assert(b >= 0); T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r; } struct UF { int n; vector<int> par; UF(int _n) : n(_n) { for(int i = 0; i < n; i++) par.push_back(i); } int find(int a){ if(a != par[a]) par[a] = find(par[a]); return par[a]; } bool join(int a, int b){ a = find(a); b = find(b); par[a] = b; return (a != b); } }; using num = modnum<int(1e9)+7>; vector<num> fact, ifact; void init(){ int N = 1100000; fact.resize(N); fact[0] = 1; for(int i = 1; i < N; i++) fact[i] = i * fact[i-1]; ifact.resize(N); ifact.back() = 1 / fact.back(); for(int i = N - 1; i > 0; i--) ifact[i-1] = i * ifact[i]; } num ncr(int n, int k){ if(k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n-k]; } int main(){ ios_base::sync_with_stdio(false), cin.tie(nullptr); init(); int N; cin >> N; int M; cin >> M; vector<int> a(N); for(int i = 0; i < N; i++){ cin >> a[i]; } UF uf(N); UF uf2(2*N); vector<pair<int,int> > edges; for(int i = 0; i < M; i++){ int u, v; cin >> u >> v; u--; v--; edges.push_back({u, v}); uf.join(u, v); uf2.join(u, v+N); uf2.join(u+N, v); } vector<vector<int> > cc(N); for(int i = 0; i < N; i++){ cc[uf.find(i)].push_back(i); } num ans = 1; for(int i = 0; i < N; i++){ if(uf.find(i) != i) continue; if(uf2.find(i) == uf2.find(i+N)){ ans *= pow(num(2), (int)cc[i].size() - 1); } else { vector<int> freq(2, 0); for(int x : cc[i]){ freq[int(uf2.find(x) == uf2.find(i)) ^ a[x]]++; } ans *= ncr(freq[0] + freq[1], freq[0]); } } 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 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 204 205 206 | #include <bits/stdc++.h> using namespace std; template <typename T> T mod_inv_in_range(T a, T m) { // assert(0 <= a && a < m); T x = a, y = m; // coeff of a in x and y T vx = 1, vy = 0; while (x) { T k = y / x; y %= x; vy -= k * vx; std::swap(x, y); std::swap(vx, vy); } assert(y == 1); return vy < 0 ? m + vy : vy; } template <typename T> struct extended_gcd_result { T gcd; T coeff_a, coeff_b; }; template <typename T> extended_gcd_result<T> extended_gcd(T a, T b) { T x = a, y = b; // coeff of a and b in x and y T ax = 1, ay = 0; T bx = 0, by = 1; while (x) { T k = y / x; y %= x; ay -= k * ax; by -= k * bx; std::swap(x, y); std::swap(ax, ay); std::swap(bx, by); } return {y, ay, by}; } template <typename T> T mod_inv(T a, T m) { a %= m; a = a < 0 ? a + m : a; return mod_inv_in_range(a, m); } template <int MOD_> struct modnum { static constexpr int MOD = MOD_; static_assert(MOD_ > 0, "MOD must be positive"); private: int v; public: modnum() : v(0) {} modnum(int64_t v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); } friend std::istream& operator >> (std::istream& in, modnum& n) { int64_t v_; in >> v_; n = modnum(v_); return in; } friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; } friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = mod_inv_in_range(v, MOD); return res; } friend modnum inv(const modnum& m) { return m.inv(); } modnum neg() const { modnum res; res.v = v ? MOD-v : 0; return res; } friend modnum neg(const modnum& m) { return m.neg(); } modnum operator- () const { return neg(); } modnum operator+ () const { return modnum(*this); } modnum& operator ++ () { v ++; if (v == MOD) v = 0; return *this; } modnum& operator -- () { if (v == 0) v = MOD; v --; return *this; } modnum& operator += (const modnum& o) { v -= MOD-o.v; v = (v < 0) ? v + MOD : v; return *this; } modnum& operator -= (const modnum& o) { v -= o.v; v = (v < 0) ? v + MOD : v; return *this; } modnum& operator *= (const modnum& o) { v = int(int64_t(v) * int64_t(o.v) % MOD); return *this; } modnum& operator /= (const modnum& o) { return *this *= o.inv(); } friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; } friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; } friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; } friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; } friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; } friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; } }; template <typename T> T pow(T a, long long b) { assert(b >= 0); T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r; } struct UF { int n; vector<int> par; UF(int _n) : n(_n) { for(int i = 0; i < n; i++) par.push_back(i); } int find(int a){ if(a != par[a]) par[a] = find(par[a]); return par[a]; } bool join(int a, int b){ a = find(a); b = find(b); par[a] = b; return (a != b); } }; using num = modnum<int(1e9)+7>; vector<num> fact, ifact; void init(){ int N = 1100000; fact.resize(N); fact[0] = 1; for(int i = 1; i < N; i++) fact[i] = i * fact[i-1]; ifact.resize(N); ifact.back() = 1 / fact.back(); for(int i = N - 1; i > 0; i--) ifact[i-1] = i * ifact[i]; } num ncr(int n, int k){ if(k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n-k]; } int main(){ ios_base::sync_with_stdio(false), cin.tie(nullptr); init(); int N; cin >> N; int M; cin >> M; vector<int> a(N); for(int i = 0; i < N; i++){ cin >> a[i]; } UF uf(N); UF uf2(2*N); vector<pair<int,int> > edges; for(int i = 0; i < M; i++){ int u, v; cin >> u >> v; u--; v--; edges.push_back({u, v}); uf.join(u, v); uf2.join(u, v+N); uf2.join(u+N, v); } vector<vector<int> > cc(N); for(int i = 0; i < N; i++){ cc[uf.find(i)].push_back(i); } num ans = 1; for(int i = 0; i < N; i++){ if(uf.find(i) != i) continue; if(uf2.find(i) == uf2.find(i+N)){ ans *= pow(num(2), (int)cc[i].size() - 1); } else { vector<int> freq(2, 0); for(int x : cc[i]){ freq[int(uf2.find(x) == uf2.find(i)) ^ a[x]]++; } ans *= ncr(freq[0] + freq[1], freq[0]); } } cout << ans << '\n'; } |