QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#23075 | #2549. King's Palace | FudanU1# | RE | 0ms | 0kb | C++17 | 3.7kb | 2022-03-11 19:29:41 | 2022-04-30 02:18:12 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include <time.h>
using namespace std;
#define int ll
#define ll long long
char cu[10],cv[10];
ll ni[1<<23|7],kk[30],kr[(1<<23)|7],kb[1<<23|7],kg[1<<23|7],qq[30],tq[30];
int f[1<<23|7];
bool b[80][80];
int n,m,zt;
/*int co[30];
ll fo[1<<22];
ll ff(int k){ll s=0;
if(k>n){int zo=0;
for(int j=1;j<=n;j++){
putchar('0'+co[j]);
if(co[j]==0)zo=zo|(1<<(j-1));
}putchar('\n');
fo[zo]++;
return 1;
}
for(int i=0;i<3;i++){
co[k]=i;
int uu=k*3+i;
bool bo=1;
for(int j=1;j<k;j++){
if(b[uu][j*3+co[j]]){bo=0;break;}
}
if(bo)s+=ff(k+1);
}return s;
}*/
signed main(){
srand(time(0));
scanf("%lld%lld",&n,&m);
for(int i=1;i<=m;i++){int u,v,uu,vv;
scanf("%d%s%d%s",&u,cu,&v,cv);
/*u=rand()%n+1;v=rand()%n+1;
int ru=rand()%3,rv=rand()%3;
if(ru==0)cu[0]='R';else if(ru==1)cu[0]='G';else cu[0]='B';
if(rv==0)cv[0]='R';else if(rv==1)cv[0]='G';else cv[0]='B';
if(u==v)continue;
cout<<u<<' '<<cu[0]<<' '<<v<<' '<<cv[0]<<endl;*/
uu=u*3;vv=v*3;
if(cu[0]=='G')uu++;if(cu[0]=='B')uu+=2;
if(cv[0]=='G')vv++;if(cv[0]=='B')vv+=2;
b[uu][vv]=1;b[vv][uu]=1;
}
//cout<<ff(1)<<endl;
//f G or B
for(int i=1;i<=n;i++)ni[1ll<<(i-1)]=i;
f[0]=1;zt=1<<n;
for(int i=1;i<zt;i++){
int w=i&-i;int lt=0;
int iw=i^w;
for(int j=1;j<=n;j++)if((i>>(j-1))&1)kk[j]=3,tq[++lt]=j;else kk[j]=0;
kk[ni[w]]=1;int lq=0;qq[lq=1]=ni[w];
int ig=iw;//cout<<'['<<lt<<endl;
while(lq){
int u=qq[lq];lq--;
int uu=u*3+kk[u];
for(int j=1;j<=lt;j++){
int kks=kk[tq[j]];
if(b[uu][tq[j]*3+1])kks&=2;
if(b[uu][tq[j]*3+2])kks&=1;
//cout<<'>'<<u<<' '<<uu<<' '<<tq[j]<<' '<<kks<<endl;
if(kks==0){lq=0;ig=zt;break;}
if(kks<kk[tq[j]]){
kk[tq[j]]=kks;
qq[++lq]=tq[j];
ig^=1ll<<(tq[j]-1);
}
}
}
for(int j=1;j<=lt;j++)kk[tq[j]]=3;
kk[ni[w]]=2;lq=0;qq[lq=1]=ni[w];
int ib=iw;
while(lq){
int u=qq[lq];lq--;
int uu=u*3+kk[u];
for(int j=1;j<=lt;j++){
int kks=kk[tq[j]];
if(b[uu][tq[j]*3+1])kks&=2;
if(b[uu][tq[j]*3+2])kks&=1;
if(kks==0){lq=0;ib=zt;break;}
if(kks<kk[tq[j]]){
kk[tq[j]]=kks;
qq[++lq]=tq[j];
ib^=1ll<<(tq[j]-1);
}
}
}
f[i]=f[ig]+f[ib];
//cout<<i<<' '<<f[i]<<' '<<ig<<' '<<ib<<endl;
}
kr[0]=zt-1;//can be red
kb[0]=kg[0]=zt-1;
ll zz=f[zt-1];//cout<<zz<<' '<<fo[0]<<endl;
for(int i=1;i<zt;i++){//i R
int w=i&-i;//cout<<kr[0]<<endl;
int iw=i^w;//cout<<i<<' '<<iw<<' '<<kr[iw]<<endl;
if(kr[iw]&w){//w can be red
kr[i]=kr[iw];
kb[i]=kb[iw];
kg[i]=kg[iw];int lq=0;int bg=(zt-1)^i;
for(int j=1;j<=n;j++)if((i>>(j-1))&1)kk[j]=4;else{
kk[j]=((kb[iw]>>(j-1))&1)<<1|((kg[iw]>>(j-1))&1);
if(kk[j]<3)qq[++lq]=j,bg^=(1<<(j-1));
}
int uu=ni[w]*3;
for(int j=1;j<=n;j++){
int kks=kk[j];
if(b[uu][j*3])kks&=3,kr[i]&=(zt-1)^(1<<(j-1));
if(b[uu][j*3+1])kks&=6,kg[i]&=(zt-1)^(1<<(j-1));
if(b[uu][j*3+2])kks&=5,kb[i]&=(zt-1)^(1<<(j-1));
if(kks==0){
bg=zt;
}
if(kks<kk[j]){
kk[j]=kks;
qq[++lq]=j;
bg^=(1ll<<(j-1));
}
}
//for(int j=1;j<=n;j++)cout<<'['<<j<<' '<<kk[j]<<endl;
while(lq&&bg<zt){
int u=qq[lq];lq--;
int uu=u*3+kk[u];
for(int j=1;j<=n;j++){
int kks=kk[j];
if(b[uu][j*3])kks&=3;
if(b[uu][j*3+1])kks&=6;
if(b[uu][j*3+2])kks&=5;
if(kks==0){bg=zt;break;}
if(kks<kk[j]){
kk[j]=kks;
qq[++lq]=j;
bg^=1ll<<(j-1);
}
}
}
zz+=f[bg];
//cout<<i<<' '<<zz<<' '<<bg<<' '<<kr[i]<<' '<<fo[i]<<' '<<f[bg]<<endl;
}else{
kr[i]=0;
//cout<<i<<' '<<fo[i]<<endl;
}
}
printf("%lld\n",zz);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 3 1 R 2 R 1 G 2 R 1 B 2 G