#include <bits/stdc++.h> using namespace std; typedef long long ll; #if DEBUG #define LOG(...) printf(__VA_ARGS__) #define DOUT cout #else #define LOG(...) #define DOUT if(0) cout #endif void dfs( const vector<vector<int>> &adj, int x, vector<bool> &visited, vector<int> &preorder ) { visited[x] = true; preorder.push_back(x); for (int u : adj[x]) { if (!visited[u]) dfs(adj, u, visited, preorder); } } struct Component { vector<int> vertices; vector<bool> arragement; vector<pair<int, int>> edges; int get_idx(int v) { for (int i = 0; i < (int)vertices.size(); i++) { if (vertices[i] == v) { return i; } } return -1; } void adjust_edges() { for (auto &e : edges) { e.first = get_idx(e.first); e.second = get_idx(e.second); } } }; void _brute( const Component &c, vector<bool> &curr_arr, set<vector<bool>> &poss_arrs ) { poss_arrs.insert(curr_arr); for (auto e : c.edges) { int a = e.first; int b = e.second; if (curr_arr[a] != curr_arr[b]) continue; curr_arr[a] = !curr_arr[a]; curr_arr[b] = !curr_arr[b]; if (poss_arrs.count(curr_arr) == 0) _brute(c, curr_arr, poss_arrs); curr_arr[a] = !curr_arr[a]; curr_arr[b] = !curr_arr[b]; } } set<vector<bool>> brute(const Component &c) { vector<bool> arr = c.arragement; set<vector<bool>> out; _brute(c, arr, out); return out; } int main() { int n, m; scanf("%d%d", &n, &m); vector<int> arr(n + 1); for (int i = 1; i <= n; i++) scanf("%d", &arr[i]); vector<vector<int>> adj(n + 1); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } // get components vector<Component> components; vector<bool> visited(n + 1, false); for (int i = 1; i <= n; i++) { if (!visited[i]) { components.emplace_back(); dfs(adj, i, visited, components.back().vertices); } } for (auto &c : components) { c.arragement.reserve(c.vertices.size()); for (int v : c.vertices) c.arragement.push_back(arr[v] == 1); for (int v : c.vertices) LOG("%d ", v); LOG("\n"); for (int a : c.arragement) LOG("%d ", a); LOG("\n"); for (int v : c.vertices) { for (int u : adj[v]) { if (u < v) { c.edges.emplace_back(u, v); LOG("%d %d\n", u, v); } } } c.adjust_edges(); LOG("\n"); } ll result = 1; for (const auto &c : components) { for (int v : c.vertices) LOG("%d ", v); LOG("\n"); auto solutions = brute(c); LOG("%ld\n", solutions.size()); for (auto arr : solutions) { for (bool v : arr) LOG("%d ", v); LOG("\n"); } result *= solutions.size(); LOG("\n"); } printf("%lld\n", result); }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #if DEBUG #define LOG(...) printf(__VA_ARGS__) #define DOUT cout #else #define LOG(...) #define DOUT if(0) cout #endif void dfs( const vector<vector<int>> &adj, int x, vector<bool> &visited, vector<int> &preorder ) { visited[x] = true; preorder.push_back(x); for (int u : adj[x]) { if (!visited[u]) dfs(adj, u, visited, preorder); } } struct Component { vector<int> vertices; vector<bool> arragement; vector<pair<int, int>> edges; int get_idx(int v) { for (int i = 0; i < (int)vertices.size(); i++) { if (vertices[i] == v) { return i; } } return -1; } void adjust_edges() { for (auto &e : edges) { e.first = get_idx(e.first); e.second = get_idx(e.second); } } }; void _brute( const Component &c, vector<bool> &curr_arr, set<vector<bool>> &poss_arrs ) { poss_arrs.insert(curr_arr); for (auto e : c.edges) { int a = e.first; int b = e.second; if (curr_arr[a] != curr_arr[b]) continue; curr_arr[a] = !curr_arr[a]; curr_arr[b] = !curr_arr[b]; if (poss_arrs.count(curr_arr) == 0) _brute(c, curr_arr, poss_arrs); curr_arr[a] = !curr_arr[a]; curr_arr[b] = !curr_arr[b]; } } set<vector<bool>> brute(const Component &c) { vector<bool> arr = c.arragement; set<vector<bool>> out; _brute(c, arr, out); return out; } int main() { int n, m; scanf("%d%d", &n, &m); vector<int> arr(n + 1); for (int i = 1; i <= n; i++) scanf("%d", &arr[i]); vector<vector<int>> adj(n + 1); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } // get components vector<Component> components; vector<bool> visited(n + 1, false); for (int i = 1; i <= n; i++) { if (!visited[i]) { components.emplace_back(); dfs(adj, i, visited, components.back().vertices); } } for (auto &c : components) { c.arragement.reserve(c.vertices.size()); for (int v : c.vertices) c.arragement.push_back(arr[v] == 1); for (int v : c.vertices) LOG("%d ", v); LOG("\n"); for (int a : c.arragement) LOG("%d ", a); LOG("\n"); for (int v : c.vertices) { for (int u : adj[v]) { if (u < v) { c.edges.emplace_back(u, v); LOG("%d %d\n", u, v); } } } c.adjust_edges(); LOG("\n"); } ll result = 1; for (const auto &c : components) { for (int v : c.vertices) LOG("%d ", v); LOG("\n"); auto solutions = brute(c); LOG("%ld\n", solutions.size()); for (auto arr : solutions) { for (bool v : arr) LOG("%d ", v); LOG("\n"); } result *= solutions.size(); LOG("\n"); } printf("%lld\n", result); } |