QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#122720 | #2617. Browsing the Collection | zswzswzsw | ML | 0ms | 0kb | C++14 | 1.6kb | 2023-07-10 22:48:50 | 2023-07-10 22:48:54 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=510;
int n,m;
int A[N][6],L[N][33],R[N][33];
int fa[N][33],d[N][33];
int res[N][N];
struct node{
int u,s;
};
int hd,tl;
node que[N*N];
int pre(int x){
--x;if(!x)x=n;return x;
}
int gF(int x,int s){return x==fa[x][s]?x:fa[x][s]=gF(fa[x][s],s);}
void update(int u,int s,int x){
if(d[u][s]==x)return;
d[u][s]=x;que[++tl]=(node){u,s};
return;
}
void Solve(int x)
{
hd=1;tl=0;
for(int i=1;i<=n;i++)
for(int j=0;j<(1<<m);j++)fa[i][j]=i,d[i][j]=n+1;
for(int i=0;i<(1<<m);i++){
d[x][i]=0;
que[++tl]=(node){x,i};
}
while(hd<=tl)
{
node tmp=que[hd++];
int u=tmp.u,s=tmp.s;
int w=d[u][s];
update(L[u][s],s,w+1);
update(R[u][s],s,w+1);
for(int i=0;i<m;i++)
if(!(s&(1<<i)))update(u,s^(1<<i),w+1);
else{
int s2=s^(1<<i),v=gF(L[u][s2],s2);
while(gF(v,s2)!=gF(L[u][s],s2)){
update(v,s2,w+1);
if(gF(L[v][s2],s2)==gF(v,s2))break;
fa[v][s2]=gF(L[v][s2],s2);
v=fa[v][s2];
}update(u,s2,w+1);
}
}
for(int i=1;i<=n;i++)if(i!=x)res[i][x]=d[i][0];
return;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=0;j<m;j++)cin>>A[i][j];
for(int s=0;s<(1<<m);s++){
for(int i=1;i<=n;i++)
{
for(int j=pre(i);j!=i;j=pre(j)){
bool tg=1;
for(int k=0;k<m;k++)
if(((1<<k)&s)&&A[i][k]!=A[j][k]){tg=0;break;}
if(tg){L[i][s]=j,R[j][s]=i;break;}
}
if(!L[i][s])L[i][s]=R[i][s]=i;
}
}
for(int i=1;i<=n;i++)Solve(i);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)cout<<res[i][j]<<' ';
puts("");
}return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Memory Limit Exceeded
input:
9 3 5 3 7 5 3 4 5 3 7 5 3 2 5 3 4 5 3 7 2 3 7 5 3 7 2 3 7