#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
using namespace std;
vector<int> adj[400001];
void create(int a, int b, string zar, set<string>& was){
for(int i=0; i<adj[a].size(); i++){
int c=adj[a][i];
if(zar[a] == zar[c]){
char toChange='0';
if(zar[a]=='0')
toChange='1';
zar[a]=toChange;
zar[c]=toChange;
if(was.find(zar)==was.end()){
was.insert(zar);
create(a, c, zar, was);
}
if(toChange=='0')
toChange='1';
else
toChange='0';
zar[a]=toChange;
zar[c]=toChange;
}
}
for(int i=0; i<adj[b].size(); i++){
int c=adj[b][i];
if(zar[b] == zar[c]){
char toChange='0';
if(zar[b]=='0')
toChange='1';
zar[b]=toChange;
zar[c]=toChange;
if(was.find(zar)==was.end()){
was.insert(zar);
create(b, c, zar, was);
}
if(toChange=='0')
toChange='1';
else
toChange='0';
zar[c]=toChange;
zar[b]=toChange;
}
}
return;
}
int main(){
int n, m;
cin >> n >> m;
string zar;
queue<pair<int, int>> qu;
set<string> was;
zar+='n';
for(int i=1; i<=n; i++){
char c;
cin >> c;
zar+=c;
}
for(int i=0; i<m; i++){
int a, b;
cin >> a >> b;
adj[a].PB(b);
adj[b].PB(a);
qu.push(MP(a, b));
}
was.insert(zar);
while(!qu.empty()){
int a, b;
a=qu.front().second;
b=qu.front().first;
qu.pop();
if(zar[a] == zar[b]){
char toChange='0';
if(zar[a]=='0')
toChange='1';
zar[a]=toChange;
zar[b]=toChange;
if(was.find(zar)==was.end()){
was.insert(zar);
create(a, b, zar, was);
}
if(toChange=='0')
toChange='1';
else
toChange='0';
zar[a]=toChange;
zar[b]=toChange;
}
}
cout << was.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 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 | #include <bits/stdc++.h> #define MP make_pair #define PB push_back using namespace std; vector<int> adj[400001]; void create(int a, int b, string zar, set<string>& was){ for(int i=0; i<adj[a].size(); i++){ int c=adj[a][i]; if(zar[a] == zar[c]){ char toChange='0'; if(zar[a]=='0') toChange='1'; zar[a]=toChange; zar[c]=toChange; if(was.find(zar)==was.end()){ was.insert(zar); create(a, c, zar, was); } if(toChange=='0') toChange='1'; else toChange='0'; zar[a]=toChange; zar[c]=toChange; } } for(int i=0; i<adj[b].size(); i++){ int c=adj[b][i]; if(zar[b] == zar[c]){ char toChange='0'; if(zar[b]=='0') toChange='1'; zar[b]=toChange; zar[c]=toChange; if(was.find(zar)==was.end()){ was.insert(zar); create(b, c, zar, was); } if(toChange=='0') toChange='1'; else toChange='0'; zar[c]=toChange; zar[b]=toChange; } } return; } int main(){ int n, m; cin >> n >> m; string zar; queue<pair<int, int>> qu; set<string> was; zar+='n'; for(int i=1; i<=n; i++){ char c; cin >> c; zar+=c; } for(int i=0; i<m; i++){ int a, b; cin >> a >> b; adj[a].PB(b); adj[b].PB(a); qu.push(MP(a, b)); } was.insert(zar); while(!qu.empty()){ int a, b; a=qu.front().second; b=qu.front().first; qu.pop(); if(zar[a] == zar[b]){ char toChange='0'; if(zar[a]=='0') toChange='1'; zar[a]=toChange; zar[b]=toChange; if(was.find(zar)==was.end()){ was.insert(zar); create(a, b, zar, was); } if(toChange=='0') toChange='1'; else toChange='0'; zar[a]=toChange; zar[b]=toChange; } } cout << was.size() << "\n"; return 0; } |
English