QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#235979#7686. The Phantom Menaceucup-team1447#WA 639ms977504kbC++145.0kb2023-11-03 14:29:322023-11-03 14:29:34

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-03 14:29:34]
  • 评测
  • 测评结果:WA
  • 用时:639ms
  • 内存:977504kb
  • [2023-11-03 14:29:32]
  • 提交

answer

// what is matter? never mind. 
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2") 
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define ull unsigned long long
#define int long long
using namespace std;
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
 
#define maxn 4000005
#define inf 0x3f3f3f3f

const ll P=2305843009213693951;
 
ull red(__int128 x) {
  ll v = (ull)(x & P) + (ull)(x >> 61) - P;
  return v + (P & (v >> 63));
}
inline ll mul(ll x,ll y){
//	return red((__int128)x*y);
	return (__int128)x*y%P;
	ll s=x*y-(ll)((long double)x*y/P)*P;
	if(s<0)return s+P;
	return (s<P?s:s-P);
}
//ll qpow(ll a,ll b){
//	ll c=1;
//	for(;b;b>>=1,a=mul(a,a))if(b&1)c=mul(c,a);
//	return c;
//}
 
#define mod P
struct modint{
	ll x;
	modint(int o=0){x=o;}
	modint &operator = (ll o){return x=o,*this;}
	modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
	modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
	modint &operator *=(modint o){return x=mul(x,o.x),*this;}
	modint &operator ^=(ll b){
		modint a=*this,c=1;
		for(;b;b>>=1,a*=a)if(b&1)c*=a;
		return x=c.x,*this;
	}
	modint &operator /=(modint o){return *this *=o^=mod-2;}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
	friend modint operator /(modint a,modint b){return a/=b;}
	friend modint operator ^(modint a,int b){return a^=b;}
	friend bool operator ==(modint a,int b){return a.x==b;}
	friend bool operator !=(modint a,int b){return a.x!=b;}
	bool operator ! () {return !x;}
	modint operator - () {return x?mod-x:0;}
	bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
 
const modint B=1145141919,ivB=modint(1)/B;
 
modint _w[maxn*2],*w=_w+maxn;
void init(int n){
	w[0]=1;
	For(i,1,n)w[i]=w[i-1]*B,w[-i]=w[-i+1]*ivB;
}

int n,m;
int resa[maxn],resb[maxn];
string a[maxn],b[maxn];
vector<modint>pa[maxn],pb[maxn],sa[maxn],sb[maxn];

void work(string&s,vector<modint>&h1,vector<modint>&h2){
	h1.resize(m);
	h2.resize(m);
	For(i,0,m-1){
		if(i)h1[i]=h1[i-1]*B+s[i];
		else h1[i]=s[i];
	}
	Rep(i,m-1,0){
		if(i!=m-1) h2[i]=h2[i+1]+s[i]*w[m-1-i];
		else h2[i]=s[i];
	}
}

int tmpa[maxn],tmpb[maxn],tt[maxn],len,cnt[maxn];
vi va[maxn],vb[maxn];
bool chk0(){
	For(i,1,n){
		tmpa[i]=pa[i].back().x;
		tmpb[i]=pb[i].back().x;
		tt[++len]=tmpa[i];
		tt[++len]=tmpb[i];
	}
	sort(tt+1,tt+len+1),len=unique(tt+1,tt+len+1)-tt-1;
	#define V(x) lower_bound(tt+1,tt+len+1,x)-tt
	
	For(i,1,len)cnt[i]=0,va[i].clear(),vb[i].clear();
	For(i,1,n){
		tmpa[i]=V(tmpa[i]);
		tmpb[i]=V(tmpb[i]);
		++cnt[tmpa[i]];
		--cnt[tmpb[i]];
	}
	For(i,1,len)if(cnt[i])return 0;
	For(i,1,n){
		va[tmpa[i]].pb(i);
		vb[tmpb[i]].pb(i);
	}
	int idx=0;
	For(i,1,len){
		while(va[i].size() && vb[i].size()){
			++idx;
			resa[idx]=va[i].back(),va[i].pop_back();
			resb[idx]=vb[i].back(),vb[i].pop_back();
		}
	}
	For(i,1,n)cout<<resa[i]<<" ";cout<<"\n";
	For(i,1,n)cout<<resb[i]<<" ";cout<<"\n";
	return 1;
}

int deg[maxn];
vector<pii>e[maxn];

void work()
{
	n=read(),m=read();
	For(i,1,n)cin>>a[i];
	For(i,1,n)cin>>b[i];
	For(i,1,n)work(a[i],pa[i],sa[i]),work(b[i],pb[i],sb[i]);
	if(chk0())return;
	For(j,1,m-1){
		len=0;
		For(i,1,n){
			tt[++len]=pa[i][j-1].x;
			tt[++len]=sa[i][j].x;
			tt[++len]=pb[i][m-j-1].x;
			tt[++len]=sb[i][m-j].x;
		}
		sort(tt+1,tt+len+1),len=unique(tt+1,tt+len+1)-tt-1;
		if(len>2*n)continue;
		
		For(i,1,len*2) e[i].clear(),deg[i]=0;
		
		For(i,1,n){
			int u=V(pa[i][j-1].x),v=V(sa[i][j].x);
			e[u].pb(mkp(v+len,i));
			++deg[u],--deg[v+len];
			u=V(pb[i][m-j-1].x),v=V(sb[i][m-j].x);
			e[u+len].pb(mkp(v,i));
			++deg[u+len],--deg[v];
		}
		bool ok=1;
		For(i,1,len*2){
			if(deg[i]){
				ok=0;
				break;
			}
		}
		if(!ok)continue;
		
		vector<pii> rec;
		function<void(int)>dfs=[&](int u){
			while(e[u].size()){
				auto [v,w]=e[u].back();
				e[u].pop_back();
				dfs(v);
				rec.pb(mkp(u,w));
			}
		};
		For(i,1,len*2)
			if(e[i].size()){
				dfs(i);
				break;
			}
		if(rec.size()!=n*2)continue;
		reverse(rec.begin(),rec.end());
		int idxa=0,idxb=0;
		for(auto [u,x]:rec){
			if(u<=n) resa[++idxa]=x;
			else resb[++idxb]=x;
		}
		For(i,1,n)cout<<resa[i]<<" ";cout<<"\n";
		For(i,1,n)cout<<resb[i]<<" ";cout<<"\n";
		return;
	}
	
	puts("-1");
}

signed main()
{
	init(1000001);
	int T=read();
	while(T--)work();
	return 0;
}
/*
3
3 3
abc
ghi
def
bcd
efg
hia
1 3
abc
def
3 3
abc
ghi
def
ghi
def
abc
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 132ms
memory: 973720kb

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: 639ms
memory: 972372kb

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: 0
Accepted
time: 412ms
memory: 977504kb

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
...

result:

ok 500000 cases (500000 test cases)

Test #4:

score: 0
Accepted
time: 420ms
memory: 972796kb

input:

500000
2 1
d
d
b
a
2 1
c
d
b
a
2 1
b
d
b
a
2 1
a
d
b
a
2 1
d
c
b
a
2 1
c
c
b
a
2 1
b
c
b
a
2 1
a
c
b
a
2 1
d
b
b
a
2 1
c
b
b
a
2 1
b
b
b
a
2 1
a
b
b
a
2 1
d
a
b
a
2 1
c
a
b
a
2 1
b
a
b
a
2 1
a
a
b
a
2 1
d
d
a
a
2 1
c
d
a
a
2 1
b
d
a
a
2 1
a
d
a
a
2 1
d
c
a
a
2 1
c
c
a
a
2 1
b
c
a
a
2 1
a
c
a
a
2 1
d...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 2 
2 1 
-1
-1
2 1 
2 1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
2 1 
2 1 
2 1 
2 1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 2 
1 2 
-1
-1
2 1 
1 2 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 2 
1 2 
-1
-1
-1
-1
-1
2 1 
1 2 
-1
-1
-1
-1
-1
-1
-1
-1
-...

result:

ok 500000 cases (500000 test cases)

Test #5:

score: -100
Wrong Answer
time: 304ms
memory: 973248kb

input:

333333
1 3
cbb
bfa
1 3
bbb
bfa
1 3
abb
bfa
1 3
fab
bfa
1 3
eab
bfa
1 3
dab
bfa
1 3
cab
bfa
1 3
bab
bfa
1 3
aab
bfa
1 3
ffa
bfa
1 3
efa
bfa
1 3
dfa
bfa
1 3
cfa
bfa
1 3
bfa
bfa
1 3
afa
bfa
1 3
fea
bfa
1 3
eea
bfa
1 3
dea
bfa
1 3
cea
bfa
1 3
bea
bfa
1 3
aea
bfa
1 3
fda
bfa
1 3
eda
bfa
1 3
dda
bfa
1 3
c...

output:

-1
-1
-1
0 
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:

wrong answer p is not permutation (test case 4)