#include <bits/stdc++.h>
using namespace std;
set<string> S;
pair<int,int> T[400100];
void rek(string s, int m){
if(S.find(s) != S.end()){
return;
}
S.insert(s);
string ns = s;
for(int i=0;i<m;i++){
if(s[T[i].first]==s[T[i].second]){
ns = s;
if(s[T[i].first]=='0'){
ns[T[i].first] = '1';
ns[T[i].second] = '1';
}
else{
ns[T[i].first] = '0';
ns[T[i].second] = '0';
}
rek(ns,m);
}
}
return;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin>>n>>m;
string nap = "$";
char a;
for(int i=0;i<n;i++){
cin>>a;
nap = nap + a;
}
for(int i=0;i<m;i++){
cin>>T[i].first>>T[i].second;
}
rek(nap, m);
cout<<S.size()<<"\n";
}
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 | #include <bits/stdc++.h> using namespace std; set<string> S; pair<int,int> T[400100]; void rek(string s, int m){ if(S.find(s) != S.end()){ return; } S.insert(s); string ns = s; for(int i=0;i<m;i++){ if(s[T[i].first]==s[T[i].second]){ ns = s; if(s[T[i].first]=='0'){ ns[T[i].first] = '1'; ns[T[i].second] = '1'; } else{ ns[T[i].first] = '0'; ns[T[i].second] = '0'; } rek(ns,m); } } return; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin>>n>>m; string nap = "$"; char a; for(int i=0;i<n;i++){ cin>>a; nap = nap + a; } for(int i=0;i<m;i++){ cin>>T[i].first>>T[i].second; } rek(nap, m); cout<<S.size()<<"\n"; } |
English