#include<bits/stdc++.h> using namespace std; void solve() { int n, m; cin >> n >> m; string g(n, 0); for(int i = 0; i < n; i++) cin >> g[i]; set<string> st; vector<pair<int, int>> edg(m); for(int i = 0; i < m; i++) { cin >> edg[i].first >> edg[i].second; edg[i].first--; edg[i].second--; } //cout << g << "\n"; queue<string> q; q.push(g); st.insert(g); //cout << st.size() << "\n"; while (!q.empty()) { string s = q.front(); q.pop(); for(auto x : edg) { if(s[x.first] == s[x.second]) { s[x.first] = '0' + (1 - (s[x.first] - '0')); s[x.second] = '0' + (1 - (s[x.second] - '0')); //cout << s << "\n"; if(!st.count(s)) { q.push(s); st.insert(s); } s[x.first] = '0' + (1 - (s[x.first] - '0')); s[x.second] = '0' + (1 - (s[x.second] - '0')); } } } cout << st.size() << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); 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 | #include<bits/stdc++.h> using namespace std; void solve() { int n, m; cin >> n >> m; string g(n, 0); for(int i = 0; i < n; i++) cin >> g[i]; set<string> st; vector<pair<int, int>> edg(m); for(int i = 0; i < m; i++) { cin >> edg[i].first >> edg[i].second; edg[i].first--; edg[i].second--; } //cout << g << "\n"; queue<string> q; q.push(g); st.insert(g); //cout << st.size() << "\n"; while (!q.empty()) { string s = q.front(); q.pop(); for(auto x : edg) { if(s[x.first] == s[x.second]) { s[x.first] = '0' + (1 - (s[x.first] - '0')); s[x.second] = '0' + (1 - (s[x.second] - '0')); //cout << s << "\n"; if(!st.count(s)) { q.push(s); st.insert(s); } s[x.first] = '0' + (1 - (s[x.first] - '0')); s[x.second] = '0' + (1 - (s[x.second] - '0')); } } } cout << st.size() << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; } |