#include <bits/stdc++.h> #define int long long #define pii pair<int,int> #define all(x) x.begin(),x.end() #define vi vector<int> #define vii vector<pii> #define vb vector<bool> #define siz(x) (int)x.size() #define pb push_back #define nd second #define st first #define rep(i,a,b) for(int i=a; i<=b; i++) using namespace std; const int maxn = 1e6, inf = 1e9, mod = 1e9+7; set<int>S; vii ruchy; void bin(int x){ int f = x,pot = 4; while(pot>=0){ if(f&(1<<pot))cout<<"1 "; else cout<<"0 "; pot--; } cout<<'\n'; } void DFS(int x){ S.insert(x); for(auto [a,b] : ruchy){ if(((x&(1<<a)) && (x&(1<<b)))||(!(x&(1<<a)) && !(x&(1<<b)))){ int akt = x; akt^=(1<<a); akt^=(1<<b); if(!(S.count(akt))){ DFS(akt); //bin(x); //bin(akt); //cout<<"\n\n"; } } } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin>>n>>m; vi mask(n); rep(i,0,n-1){ cin>>mask[i]; } reverse(all(mask)); int start = 0; rep(i,0,n-1)start+=(mask[i]*(1<<i)); rep(i,0,m-1){ int a,b; cin>>a>>b; ruchy.pb({a-1,b-1}); } DFS(start); //for(auto w : S)bin(w); cout<<(siz(S))%mod; }
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 | #include <bits/stdc++.h> #define int long long #define pii pair<int,int> #define all(x) x.begin(),x.end() #define vi vector<int> #define vii vector<pii> #define vb vector<bool> #define siz(x) (int)x.size() #define pb push_back #define nd second #define st first #define rep(i,a,b) for(int i=a; i<=b; i++) using namespace std; const int maxn = 1e6, inf = 1e9, mod = 1e9+7; set<int>S; vii ruchy; void bin(int x){ int f = x,pot = 4; while(pot>=0){ if(f&(1<<pot))cout<<"1 "; else cout<<"0 "; pot--; } cout<<'\n'; } void DFS(int x){ S.insert(x); for(auto [a,b] : ruchy){ if(((x&(1<<a)) && (x&(1<<b)))||(!(x&(1<<a)) && !(x&(1<<b)))){ int akt = x; akt^=(1<<a); akt^=(1<<b); if(!(S.count(akt))){ DFS(akt); //bin(x); //bin(akt); //cout<<"\n\n"; } } } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m; cin>>n>>m; vi mask(n); rep(i,0,n-1){ cin>>mask[i]; } reverse(all(mask)); int start = 0; rep(i,0,n-1)start+=(mask[i]*(1<<i)); rep(i,0,m-1){ int a,b; cin>>a>>b; ruchy.pb({a-1,b-1}); } DFS(start); //for(auto w : S)bin(w); cout<<(siz(S))%mod; } |