QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#246429 | #7686. The Phantom Menace | do_while_true | TL | 608ms | 470452kb | C++14 | 4.7kb | 2023-11-10 20:15:29 | 2023-11-10 20:15:30 |
Judging History
你现在查看的是最新测评结果
- [2024-10-08 14:11:03]
- hack成功,自动添加数据
- (/hack/941)
- [2024-10-08 10:05:28]
- hack成功,自动添加数据
- (/hack/940)
- [2024-10-07 19:51:15]
- hack成功,自动添加数据
- (/hack/938)
- [2024-10-07 19:28:01]
- hack成功,自动添加数据
- (/hack/937)
- [2024-10-07 17:16:32]
- hack成功,自动添加数据
- (/hack/936)
- [2024-10-07 16:53:09]
- hack成功,自动添加数据
- (/hack/935)
- [2024-10-07 16:22:17]
- hack成功,自动添加数据
- (/hack/934)
- [2023-11-10 20:15:29]
- 提交
answer
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<map>
#include<random>
#include<assert.h>
#include<functional>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n'
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n'
#define DE(fmt,...) fprintf(stderr, "Line %d : " fmt "\n",__LINE__,##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
r=0;bool w=0;char ch=getchar();
while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
const ll mod1=1000000007;
const ll mod2=998244353;
const ll base1=233;
const ll base2=277;
const int N=2000010;
int n,m;
int p[N],q[N];
string a[N],b[N];
ll bas1[N],bas2[N];
vll sa1[N],sa2[N];
vll sb1[N],sb2[N];
pll qrya(int i,int l,int r){
return mp(
(sa1[i][r]-sa1[i][l-1]*bas1[r-l+1]%mod1+mod1)%mod1,
(sa2[i][r]-sa2[i][l-1]*bas2[r-l+1]%mod2+mod2)%mod2
);
}
pll qryb(int i,int l,int r){
return mp(
(sb1[i][r]-sb1[i][l-1]*bas1[r-l+1]%mod1+mod1)%mod1,
(sb2[i][r]-sb2[i][l-1]*bas2[r-l+1]%mod2+mod2)%mod2
);
}
int tot;
vpii eg1[N],eg2[N],eg[N];
int head[N],vis[N],in[N];
vi vec1,vec2;
int fa[N];
int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}
void merge(int x,int y){fa[getfa(x)]=getfa(y);}
void solve(){
read(n,m);
bas1[0]=bas2[0]=1;
for(int i=1;i<=m;i++){
bas1[i]=bas1[i-1]*base1%mod1;
bas2[i]=bas2[i-1]*base2%mod2;
}
for(int i=1;i<=n;i++){
cin>>a[i];
a[i]=' '+a[i];
sa1[i].resize(m+1);
sa2[i].resize(m+1);
for(int j=1;j<=m;j++){
sa1[i][j]=(sa1[i][j-1]*base1+a[i][j])%mod1;
sa2[i][j]=(sa2[i][j-1]*base2+a[i][j])%mod2;
}
}
for(int i=1;i<=n;i++){
cin>>b[i];
b[i]=' '+b[i];
sb1[i].resize(m+1);
sb2[i].resize(m+1);
for(int j=1;j<=m;j++){
sb1[i][j]=(sb1[i][j-1]*base1+b[i][j])%mod1;
sb2[i][j]=(sb2[i][j-1]*base2+b[i][j])%mod2;
}
}
for(int i=1;i<=n;i++)p[i]=q[i]=1;
sort(p+1,p+n+1,[&](int &x,int &y){return qrya(x,1,m)<qrya(y,1,m);});
sort(q+1,q+n+1,[&](int &x,int &y){return qryb(x,1,m)<qryb(y,1,m);});
int fl=1;
for(int i=1;i<=n;i++)if(qrya(i,1,m)!=qryb(i,1,m)){fl=0;break;}
if(fl){
for(int i=1;i<=n;i++)cout<<p[i]<<' ';
puts("");
for(int i=1;i<=n;i++)cout<<q[i]<<' ';
puts("");
return ;
}
for(int d=1;d<m;d++){
map<pll,int>v1,v2;
int c1=0,c2=0;
for(int i=1;i<=n;i++){
auto l=qrya(i,1,d),r=qrya(i,d+1,m);
if(!v1[l])v1[l]=++c1;
if(!v2[r])v2[r]=++c2;
eg1[v1[l]].pb(mp(v2[r],i));
}
for(int i=1;i<=n;i++){
auto l=qryb(i,1,m-d),r=qryb(i,m-d+1,m);
if(!v2[l])v2[l]=++c2;
if(!v1[r])v1[r]=++c1;
eg2[v2[l]].pb(mp(v1[r],i));
}
tot=c1+c2;
for(int i=1;i<=tot;i++)fa[i]=i;
for(int x=1;x<=c1;x++){
for(auto v:eg1[x]){
v.fi+=c1;
eg[x].pb(v);
merge(x,v.fi+c1);
}
}
for(int x=1;x<=c2;x++){
for(auto v:eg2[x]){
v.se+=n;
eg[x+c1].pb(v);
merge(x+c1,v.fi);
}
}
int lt=1;
for(int i=2;i<=tot;i++)if(getfa(i)!=getfa(lt)){lt=0;break;}
for(int i=1;i<=tot;i++)in[i]=0;
for(int i=1;i<=tot;i++)
for(auto j:eg[i])
in[j.fi]++;
for(int i=1;i<=tot;i++)
if(in[i]!=(int)eg[i].size())
lt=0;
if(!lt)continue;
for(int i=1;i<=tot;i++)head[i]=0;
for(int i=1;i<=2*n;i++)vis[i]=0;
vi().swap(vec1);
vi().swap(vec2);
function<void(int)>dfs=[&](int x){
for(int &i=head[x];i<(int)eg[x].size();i++){
int v=eg[x][i].fi,id=eg[x][i].se;
if(!vis[id]){
vis[id]=1;
dfs(v);
if(id<=n)vec1.pb(id);
else vec2.pb(id-n);
}
}
};
dfs(1);
reverse(vec1.begin(),vec1.end());
reverse(vec2.begin(),vec2.end());
for(auto i:vec1)cout<<i<<' ';
puts("");
for(auto i:vec2)cout<<i<<' ';
puts("");
return ;
}
puts("-1");
}
signed main(){
#ifdef do_while_true
// assert(freopen("data.in","r",stdin));
// assert(freopen("data.out","w",stdout));
#endif
int T;read(T);
while(T--)solve();
#ifdef do_while_true
// cerr<<'\n'<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC*1000<<" ms"<<'\n';
#endif
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 75ms
memory: 470452kb
input:
2 3 3 abc ghi def bcd efg hia 1 3 abc def
output:
1 3 2 1 2 3 -1
result:
ok 2 cases (2 test cases)
Test #2:
score: 0
Accepted
time: 608ms
memory: 466356kb
input:
1000000 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 b b 1 1 a b 1 1 b a 1 1 a a 1 1 ...
output:
1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 ...
result:
ok 1000000 cases (1000000 test cases)
Test #3:
score: -100
Time Limit Exceeded
input:
500000 1 2 dd ba 1 2 cd ba 1 2 bd ba 1 2 ad ba 1 2 dc ba 1 2 cc ba 1 2 bc ba 1 2 ac ba 1 2 db ba 1 2 cb ba 1 2 bb ba 1 2 ab ba 1 2 da ba 1 2 ca ba 1 2 ba ba 1 2 aa ba 1 2 dd aa 1 2 cd aa 1 2 bd aa 1 2 ad aa 1 2 dc aa 1 2 cc aa 1 2 bc aa 1 2 ac aa 1 2 db aa 1 2 cb aa 1 2 bb aa 1 2 ab aa 1 2 da aa 1 2...
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...