QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#642000 | #5665. AA Country and King Dreamoon | vwxyz | WA | 259ms | 10620kb | Python3 | 2.4kb | 2024-10-15 08:31:45 | 2024-10-15 08:31:45 |
Judging History
answer
T=int(input())
for t in range(T):
N=int(input())
A=list(map(int,input().split()))
for i in range(2*N-1):
A[i]-=1
def solve(A):
if A[0]==-1:
A[0]=0
if A[-1]==-1:
A[-1]=0
if not -1 in A:
return A
parents=[None]*N
seen=[0]*N
for a in A:
if a>=0:
seen[a]=1
idx=[i for i in range(2*N-1) if A[i]==-1]
for a,b in zip(A,A[1:]):
if b==-1:
break
if a==0:
parents[b]=a
elif b==0:
parents[a]=b
else:
if parents[b]==None:
parents[b]=a
for a,b in zip(A[::-1],A[::-1][1:]):
if b==-1:
break
if a==0:
parents[b]=a
elif b==0:
parents[a]=b
else:
if parents[b]==None:
parents[b]=a
tour0=[A[idx[0]-1]]
tour1=[A[idx[-1]+1]]
while parents[tour0[-1]]!=None:
tour0.append(parents[tour0[-1]])
while parents[tour1[-1]]!=None:
tour1.append(parents[tour1[-1]])
while tour0 and tour1 and tour0[-1]==tour1[-1]:
lca=tour0.pop()
tour1.pop()
tour=tour0+[lca]+tour1[::-1]
queue=[x for x in range(N-1,-1,-1) if not seen[x]]
le=len(tour)
i=0
x=tour[0]
lst=[]
while i<le-1 or queue:
if tour[i]==x:
if i==le-1:
parents[queue[-1]]=x
x=queue.pop()
elif not queue:
i+=1
x=tour[i]
elif tour[i+1]<queue[-1]:
i+=1
x=tour[i]
else:
parents[queue[-1]]=x
x=queue.pop()
else:
if not queue:
x=parents[x]
elif parents[x]<queue[-1]:
x=parents[x]
else:
parents[queue[-1]]=x
x=queue.pop()
lst.append(x)
for i,x in zip(idx,lst):
A[i]=x
return A
ans_lst=solve(A)
print(*[ans+1 for ans in ans_lst])
详细
Test #1:
score: 100
Accepted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