#include "bits/stdc++.h" using namespace std; int n; unordered_set<vector<bool>> vis; vector<vector<int>> G; void dfs(vector<bool> states){ vis.insert(states); for(int i=0; i<n; i++){ for(auto v : G[i]){ if(states[i] == states[v]){ auto tmp = states; tmp[i] = tmp[v] = !states[i]; if(!vis.count(tmp)){ dfs(tmp); } } } } } int main() { ios::sync_with_stdio(0); cin.tie(0); int m; cin >> n >> m; G.resize(n); vector<bool> states(n); for(int i=0; i<n; i++) {int x; cin >> x; states[i] = (x!=0); } while(m--){ int a, b; cin >> a >> b; a--; b--; G[a].push_back(b); G[b].push_back(a); } dfs(states); cout << vis.size() << '\n'; 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 | #include "bits/stdc++.h" using namespace std; int n; unordered_set<vector<bool>> vis; vector<vector<int>> G; void dfs(vector<bool> states){ vis.insert(states); for(int i=0; i<n; i++){ for(auto v : G[i]){ if(states[i] == states[v]){ auto tmp = states; tmp[i] = tmp[v] = !states[i]; if(!vis.count(tmp)){ dfs(tmp); } } } } } int main() { ios::sync_with_stdio(0); cin.tie(0); int m; cin >> n >> m; G.resize(n); vector<bool> states(n); for(int i=0; i<n; i++) {int x; cin >> x; states[i] = (x!=0); } while(m--){ int a, b; cin >> a >> b; a--; b--; G[a].push_back(b); G[b].push_back(a); } dfs(states); cout << vis.size() << '\n'; return 0; } |