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
| #include<bits/stdc++.h> using namespace std; #define ll long long const int maxn=22; const int maxm=1<<maxn; int n,m; int lg[maxm]; vector<int> ban[3][3][maxn]; bool ban2[maxn+5][maxn+5]; bool flg[maxn][2]; int f[maxm]; ll ans; inline int Lowbit(int x){return x&(-x);} inline void Delete(int p,int c){ for(int j:ban[c][0][p]) flg[j][0]=1; for(int j:ban[c][1][p]) flg[j][1]=1; } inline int Find(int s){ for(;s;s^=Lowbit(s)){ int i=lg[Lowbit(s)]; if(flg[i][0]==1&&flg[i][1]==0) return i<<1|1; if(flg[i][0]==0&&flg[i][1]==1) return i<<1; if(flg[i][0]==1&&flg[i][1]==1) return -1; } return -2; } inline int Calc(int s){ int x; while(true){ x=Find(s); if(x==-2) return s; if(x==-1) return -1; s^=1<<(x>>1); Delete(x>>1,x&1); } } signed main(){ int a,b,x,y; scanf("%d%d",&n,&m); for(int i=0,t=1;i<n;i++,t<<=1) lg[t]=i; while(m--){ scanf("%d%d%d%d",&a,&x,&b,&y); a--,b--; ban[x][y][a].emplace_back(b); ban[y][x][b].emplace_back(a); if(x==2&&y==2) ban2[a][b]=ban2[b][a]=1; } int t; f[0]=1; for(int i=1;i<(1<<n);i++){ memset(flg,0,sizeof(flg)); Delete(lg[Lowbit(i)],0); t=Calc(i^Lowbit(i)); if(~t) f[i]+=f[t]; memset(flg,0,sizeof(flg)); Delete(lg[Lowbit(i)],1); t=Calc(i^Lowbit(i)); if(~t) f[i]+=f[t]; } for(int i=0;i<(1<<n);i++){ bool chk=0; for(int j=i;j;j^=Lowbit(j)) for(int k=j^Lowbit(j);k;k^=Lowbit(k)) chk|=ban2[lg[Lowbit(j)]][lg[Lowbit(k)]]; if(chk) continue; memset(flg,0,sizeof(flg)); for(int j=i;j;j^=Lowbit(j)) Delete(lg[Lowbit(j)],2); t=Calc(((1<<n)-1)^i); if(~t) ans+=f[t]; } printf("%lld",ans); return 0; }
|