#pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PPC(x) __builtin_popcountll(x) #define MSB(x) (63 - __builtin_clzll(x)) #define LSB(x) __builtin_ctzll(x) #define ARG(x, i) (get<i>(x)) #define ithBit(m, i) ((m) >> (i) & 1) #define pb push_back #define ft first #define sd second #define kw(a) ((a) * (a)) #define CLR(x) x.clear(), x.shrink_to_fit() #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 1 << 18, mod = 1'000'000'007; template <typename T1, typename T2> inline void addMod(T1& a, T2 b) { a = (a + b) % mod; } template <typename T1, typename T2> inline void multMod(T1& a, T2 b) { a = a * b % mod; } long long qpow(long long a, long long b) { long long ret = 1ll; for (; b != 0; b /= 2) { if (b & 1) multMod(ret, a); multMod(a, a); } return ret; } template <typename T1, typename T2> inline void subMod(T1& a, T2 b) { a = (a - b%mod + mod) % mod; } template <typename T1, typename T2> inline void divMod(T1& a, T2 b) { a = a * qpow(b, mod-2) % mod; } long long fact[maxN], tcaf[maxN], pow2[maxN]; long long cf(int n, int k) { static bool initd = false; if (!initd) { fact[0] = tcaf[0] = 1ll; FOR(i, 1, maxN) fact[i] = fact[i-1] * i % mod; tcaf[maxN-1] = qpow(fact[maxN-1], mod-2); FORD(i, maxN-1, 0) tcaf[i-1] = tcaf[i] * i % mod; initd = true; } if (k < 0 or k > n) return 0; long long ret = fact[n]; multMod(ret, tcaf[k]); multMod(ret, tcaf[n-k]); return ret; } bool vis[maxN]; int myColor[maxN], C[maxN]; int compSz, k; bool oddCycle; vector <int> G[maxN]; void dfs(int v) { vis[v] = true; compSz++; k += myColor[v] != C[v]; for (int u : G[v]) { if (vis[u]) { if (myColor[v] == myColor[u]) oddCycle = true; } else { myColor[u] = myColor[v] ^ 1; dfs(u); } } } void solve() { int n, m; scanf ("%d%d", &n, &m); pow2[0] = 1ll; FOR(i, 1, n+1) scanf ("%d", C+i), pow2[i] = pow2[i-1] * 2 % mod; while (m--) { int a, b; scanf ("%d%d", &a, &b); G[a].pb(b); G[b].pb(a); } long long res = 1; FOR(i, 1, n+1) if (!vis[i]) { dfs(i); long long resLocal = 1; if (oddCycle) resLocal = pow2[compSz - 1]; else resLocal = cf(compSz, k); multMod(res, resLocal); k = compSz = 0; oddCycle = false; } printf("%lld\n", res); } int main() { int t = 1; // scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | #pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PPC(x) __builtin_popcountll(x) #define MSB(x) (63 - __builtin_clzll(x)) #define LSB(x) __builtin_ctzll(x) #define ARG(x, i) (get<i>(x)) #define ithBit(m, i) ((m) >> (i) & 1) #define pb push_back #define ft first #define sd second #define kw(a) ((a) * (a)) #define CLR(x) x.clear(), x.shrink_to_fit() #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 1 << 18, mod = 1'000'000'007; template <typename T1, typename T2> inline void addMod(T1& a, T2 b) { a = (a + b) % mod; } template <typename T1, typename T2> inline void multMod(T1& a, T2 b) { a = a * b % mod; } long long qpow(long long a, long long b) { long long ret = 1ll; for (; b != 0; b /= 2) { if (b & 1) multMod(ret, a); multMod(a, a); } return ret; } template <typename T1, typename T2> inline void subMod(T1& a, T2 b) { a = (a - b%mod + mod) % mod; } template <typename T1, typename T2> inline void divMod(T1& a, T2 b) { a = a * qpow(b, mod-2) % mod; } long long fact[maxN], tcaf[maxN], pow2[maxN]; long long cf(int n, int k) { static bool initd = false; if (!initd) { fact[0] = tcaf[0] = 1ll; FOR(i, 1, maxN) fact[i] = fact[i-1] * i % mod; tcaf[maxN-1] = qpow(fact[maxN-1], mod-2); FORD(i, maxN-1, 0) tcaf[i-1] = tcaf[i] * i % mod; initd = true; } if (k < 0 or k > n) return 0; long long ret = fact[n]; multMod(ret, tcaf[k]); multMod(ret, tcaf[n-k]); return ret; } bool vis[maxN]; int myColor[maxN], C[maxN]; int compSz, k; bool oddCycle; vector <int> G[maxN]; void dfs(int v) { vis[v] = true; compSz++; k += myColor[v] != C[v]; for (int u : G[v]) { if (vis[u]) { if (myColor[v] == myColor[u]) oddCycle = true; } else { myColor[u] = myColor[v] ^ 1; dfs(u); } } } void solve() { int n, m; scanf ("%d%d", &n, &m); pow2[0] = 1ll; FOR(i, 1, n+1) scanf ("%d", C+i), pow2[i] = pow2[i-1] * 2 % mod; while (m--) { int a, b; scanf ("%d%d", &a, &b); G[a].pb(b); G[b].pb(a); } long long res = 1; FOR(i, 1, n+1) if (!vis[i]) { dfs(i); long long resLocal = 1; if (oddCycle) resLocal = pow2[compSz - 1]; else resLocal = cf(compSz, k); multMod(res, resLocal); k = compSz = 0; oddCycle = false; } printf("%lld\n", res); } int main() { int t = 1; // scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } return 0; } |