#include <bits/stdc++.h> using namespace std; const int MXN=5e5+1; int a[MXN], b[MXN], ans, n, m; //~ bool odw[1<<29]; unordered_set<long long>odw; void solve(long long w) { odw.insert(w); ans++; for (int i=0; i<m; i++) { long long x=(1ll<<a[i]), y=(1ll<<b[i]); if (!(w&x)==!(w&y) && odw.find(w^x^y)==odw.end()) { solve(w^x^y); } } } int main() { cin>>n>>m; long long start=0; for (int i=0; i<n; i++) { int c; cin>>c; start+=c*(1ll<<i); } for (int i=0; i<m; i++) { cin>>a[i]>>b[i]; a[i]--; b[i]--; } solve(start); cout<<ans<<"\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 | #include <bits/stdc++.h> using namespace std; const int MXN=5e5+1; int a[MXN], b[MXN], ans, n, m; //~ bool odw[1<<29]; unordered_set<long long>odw; void solve(long long w) { odw.insert(w); ans++; for (int i=0; i<m; i++) { long long x=(1ll<<a[i]), y=(1ll<<b[i]); if (!(w&x)==!(w&y) && odw.find(w^x^y)==odw.end()) { solve(w^x^y); } } } int main() { cin>>n>>m; long long start=0; for (int i=0; i<n; i++) { int c; cin>>c; start+=c*(1ll<<i); } for (int i=0; i<m; i++) { cin>>a[i]>>b[i]; a[i]--; b[i]--; } solve(start); cout<<ans<<"\n"; } |