QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#526365 | #5665. AA Country and King Dreamoon | solar_express | WA | 26ms | 7860kb | C++14 | 1.6kb | 2024-08-21 14:15:15 | 2024-08-21 14:15:15 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int T,n;
int o[600005];
int fa[320000],dep[320000];
int pd[320000];
int zan[320000],ztop=0;
void in(int x,int y){
if(x==y)return ;
in(fa[x],y);
zan[++ztop]=x;
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
while(dep[x]>dep[y])x=fa[x];
while(x!=y){
x=fa[x];y=fa[y];
}
return x;
}
void sol(int x,int y){
int lca=LCA(x,y);
while(y!=lca){
zan[++ztop]=y;
y=fa[y];
}
zan[++ztop]=lca;
in(x,lca);
}
int main(){
//freopen("ans.out","w",stdout);
cin>>T;
while(T--){
scanf("%d",&n);
ztop=0;
for(int i=1;i<=n;i++)pd[i]=0;
for(int i=1;i<2*n;i++){
scanf("%d",&o[i]);
pd[o[i]]++;
}
o[1]=1;
o[2*n-1]=1;
pd[1]=2;
int now1=0,reg=1;
for(int i=1;i<2*n;i++){
if(!o[i])break;
if(now1&&fa[now1]==o[i]){
now1=o[i];
}
else{
fa[o[i]]=now1;
dep[o[i]]=dep[now1]+1;
now1=o[i];
}
reg=i;
}
int now2=0;
for(int i=2*n-1;i;i--){
if(!o[i])break;
if(now2&&fa[now2]==o[i]){
now2=o[i];
}
else{
fa[o[i]]=now2;
dep[o[i]]=dep[now2]+1;
now2=o[i];
}
}
int zz=1;while(pd[zz])zz++;
sol(now1,now2);
while(ztop){
if(ztop==1){
for(int i=zz;i<=n;i++){
if(pd[i])continue;
o[++reg]=i;
o[++reg]=zan[ztop];
}
break;
}
if(zan[ztop-1]>zz){
zan[++ztop]=zz;
o[++reg]=zz;
zz++;
while(pd[zz])zz++;
}
else{
o[++reg]=zan[ztop-1];
ztop--;
}
}
for(int i=1;i<2*n;i++){
printf("%d ",o[i]);
}puts("");
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5924kb
input:
9 5 1 2 3 2 0 2 1 5 1 5 1 2 3 0 0 2 1 5 1 5 1 2 0 0 0 2 1 5 1 5 1 2 0 0 0 0 1 5 1 5 1 0 0 0 0 0 1 5 1 5 1 0 0 0 0 0 0 5 1 5 1 0 0 0 0 0 0 0 1 5 1 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0
output:
1 2 3 2 4 2 1 5 1 1 2 3 2 4 2 1 5 1 1 2 3 2 4 2 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1
result:
ok 9 lines
Test #2:
score: -100
Wrong Answer
time: 26ms
memory: 7860kb
input:
28668 2 0 2 1 2 0 0 1 2 0 0 0 2 1 0 1 2 1 0 0 2 1 2 0 3 0 2 1 3 1 3 0 0 1 3 1 3 0 0 0 3 1 3 0 0 0 0 1 3 0 0 0 0 0 3 1 0 1 3 1 3 1 0 0 3 1 3 1 0 0 0 1 3 1 0 0 0 0 3 1 2 0 3 1 3 1 2 0 0 1 3 1 2 0 0 0 3 1 2 1 0 1 3 1 2 1 0 0 3 1 2 1 3 0 3 0 2 3 2 1 3 0 0 3 2 1 3 0 0 0 2 1 3 1 0 3 2 1 3 1 0 0 2 1 3 1 2 ...
output:
1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 1 3 1 1 2 3 2 1 1 2 3 2 1 1 2 3 2 1 1 2 3 2 1 1 2 3 2 1 1 2 3 2 1 1 2 3 2 1 1 2 3 2 1 1 2 3...
result:
wrong answer 428th lines differ - expected: '1 4 2 3 2 4 1', found: '1 4 2 4 3 4 1 '