显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
int T,n,a[500005],vis[500005],i1,i2,lose,ans[500005],hd,tl;
int main() {
freopen("permutation.in","r",stdin);
freopen("permutation.out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>T;
while(T--) {
i1=i2=lose=0;
cin>>n;
for(int i=1;i<=n;++i) {
ans[i]=0;
if(i<n)cin>>a[i];
vis[i]=0;
}
for(int i=1;i<n-1;++i) {
if(vis[a[i]]) {
lose=1;
break;
}
vis[a[i]]=1;
}
if(a[n-1]!=1) lose=1;
if(lose) {
cout<<-1<<endl;
continue;
}
for(int i=1;i<=n;++i) {
if(vis[i]) continue;
if(!i1) i1=i;
else i2=i;
}
hd=1,tl=n;
ans[1]=i1,ans[n]=i2;
for(int i=1;i<n-1;++i) {
if(ans[tl]>ans[hd]) ans[--tl]=a[i];
else ans[++hd]=a[i];
}
for(int i=1;i<=n;++i)cout<<ans[i]<<" ";
cout<<endl;
}
return 0;
}