#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; ll dfs(ll node, vector<pii> &edges, vector<bool> &visited, int n) { if (visited[node]) { return 0; } visited[node] = true; // for (int i = 0; i < n; ++i) { // cerr << ((node & (1L << i)) != 0); // } // cerr << endl; ll result = 1; for (size_t i = 0; i < edges.size(); ++i) { int a = edges[i].first; int b = edges[i].second; bool is_a_set = node & (1L << a); bool is_b_set = node & (1L << b); if (!(is_a_set ^ is_b_set)) { ll new_node = node ^ (1L << a) ^ (1L << b); result += dfs(new_node, edges, visited, n); } } return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<bool> node(n); for (int i = 0; i < n; ++i) { int n_i; cin >> n_i; node[i] = n_i; } vector<pii> edges(m); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; edges[i] = make_pair(a - 1, b - 1); // edges[a - 1].push_back(b - 1); // edges[b - 1].push_back(a - 1); } ll bitset = 0; for (int i = 0; i < n; ++i) { bitset |= (node[i] << i); } vector<bool> visited((1L << n)); ll result = dfs(bitset, edges, visited, n); cout << result << endl; 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; ll dfs(ll node, vector<pii> &edges, vector<bool> &visited, int n) { if (visited[node]) { return 0; } visited[node] = true; // for (int i = 0; i < n; ++i) { // cerr << ((node & (1L << i)) != 0); // } // cerr << endl; ll result = 1; for (size_t i = 0; i < edges.size(); ++i) { int a = edges[i].first; int b = edges[i].second; bool is_a_set = node & (1L << a); bool is_b_set = node & (1L << b); if (!(is_a_set ^ is_b_set)) { ll new_node = node ^ (1L << a) ^ (1L << b); result += dfs(new_node, edges, visited, n); } } return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<bool> node(n); for (int i = 0; i < n; ++i) { int n_i; cin >> n_i; node[i] = n_i; } vector<pii> edges(m); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; edges[i] = make_pair(a - 1, b - 1); // edges[a - 1].push_back(b - 1); // edges[b - 1].push_back(a - 1); } ll bitset = 0; for (int i = 0; i < n; ++i) { bitset |= (node[i] << i); } vector<bool> visited((1L << n)); ll result = dfs(bitset, edges, visited, n); cout << result << endl; return 0; } |