#include <bits/stdc++.h> using namespace std; constexpr int P = 1000000007; int inv(int a) { int x = 1, y = 0, q = P; int pos = 0; while (a > 0) { int r = q % a; q = q/a; int h = q*x + y; y = x; x = h; q = a; a = r; pos = !pos; } return pos ? y : (P - y); } int n; vector<int> color; vector<vector<int>> graph; vector<int> visited; vector<int> component; void AddEdge(int a, int b) { graph[a].push_back(b); } void Read() { int m; scanf("%d%d", &n, &m); color.resize(n); graph.resize(n); for (int i = 0; i < n; i++) scanf("%d", &color[i]); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; AddEdge(a, b); AddEdge(b, a); } } bool DFS(int a, int depth = 1) { component.push_back(a); visited[a] = depth; bool is_bipartite = true; for (int b : graph[a]) if (visited[b]) is_bipartite &= depth != visited[b]; else is_bipartite &= DFS(b, depth ^ 3); return is_bipartite; } int Biparitie() { int count[2][2] = {{0, 0}, {0, 0}}; for (int a : component) count[visited[a] - 1][color[a]]++; int m[2]; for (int i = 0; i < 2; i++) m[i] = count[i][0] + count[i][1]; int k[2]; { int min_k = min(count[0][0], count[1][0]); for (int i = 0; i < 2; i++) k[i] = count[i][0] - min_k; } int64_t binom = 1; for (int i = 0; i < 2; i++) { for (int j = 1; j <= k[i]; j++) { binom = (binom * (m[i] - j + 1)) % P; binom = (binom * inv(j)) % P; } } int res = 0; for (; k[0] <= m[0] && k[1] <= m[1]; k[0]++, k[1]++) { res = (res + binom) % P; for (int i = 0; i < 2; i++) { binom = (binom * (m[i] - k[i])) % P; binom = (binom * inv(k[i] + 1)) % P; } } return res; } int Connected() { int res = 1; for (int i = 0; i < (int) component.size() - 1; i++) res = (res * 2) % P; return res; } int64_t SolveComponent(int r) { component.clear(); bool is_bipartite = DFS(r); if (is_bipartite) return Biparitie(); else return Connected(); } int Solve() { int total = 1; visited.resize(n, 0); for (int i = 0; i < n; i++) if (!visited[i]) total = (SolveComponent(i) * total) % P; return total; } int main() { Read(); printf("%d\n", 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 | #include <bits/stdc++.h> using namespace std; constexpr int P = 1000000007; int inv(int a) { int x = 1, y = 0, q = P; int pos = 0; while (a > 0) { int r = q % a; q = q/a; int h = q*x + y; y = x; x = h; q = a; a = r; pos = !pos; } return pos ? y : (P - y); } int n; vector<int> color; vector<vector<int>> graph; vector<int> visited; vector<int> component; void AddEdge(int a, int b) { graph[a].push_back(b); } void Read() { int m; scanf("%d%d", &n, &m); color.resize(n); graph.resize(n); for (int i = 0; i < n; i++) scanf("%d", &color[i]); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; AddEdge(a, b); AddEdge(b, a); } } bool DFS(int a, int depth = 1) { component.push_back(a); visited[a] = depth; bool is_bipartite = true; for (int b : graph[a]) if (visited[b]) is_bipartite &= depth != visited[b]; else is_bipartite &= DFS(b, depth ^ 3); return is_bipartite; } int Biparitie() { int count[2][2] = {{0, 0}, {0, 0}}; for (int a : component) count[visited[a] - 1][color[a]]++; int m[2]; for (int i = 0; i < 2; i++) m[i] = count[i][0] + count[i][1]; int k[2]; { int min_k = min(count[0][0], count[1][0]); for (int i = 0; i < 2; i++) k[i] = count[i][0] - min_k; } int64_t binom = 1; for (int i = 0; i < 2; i++) { for (int j = 1; j <= k[i]; j++) { binom = (binom * (m[i] - j + 1)) % P; binom = (binom * inv(j)) % P; } } int res = 0; for (; k[0] <= m[0] && k[1] <= m[1]; k[0]++, k[1]++) { res = (res + binom) % P; for (int i = 0; i < 2; i++) { binom = (binom * (m[i] - k[i])) % P; binom = (binom * inv(k[i] + 1)) % P; } } return res; } int Connected() { int res = 1; for (int i = 0; i < (int) component.size() - 1; i++) res = (res * 2) % P; return res; } int64_t SolveComponent(int r) { component.clear(); bool is_bipartite = DFS(r); if (is_bipartite) return Biparitie(); else return Connected(); } int Solve() { int total = 1; visited.resize(n, 0); for (int i = 0; i < n; i++) if (!visited[i]) total = (SolveComponent(i) * total) % P; return total; } int main() { Read(); printf("%d\n", Solve()); return 0; } |