QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#641996#5665. AA Country and King DreamoonvwxyzRE 11ms10700kbPython32.1kb2024-10-15 08:27:302024-10-15 08:27:30

Judging History

你现在查看的是最新测评结果

  • [2024-10-15 08:27:30]
  • 评测
  • 测评结果:RE
  • 用时:11ms
  • 内存:10700kb
  • [2024-10-15 08:27:30]
  • 提交

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
        for a,b in zip(A,A[1:]):
            if a>=0 and b>=0:
                if a==0:
                    parents[b]=a
                elif b==0:
                    parents[a]=b
                else:
                    if parents[b]==None:
                        parents[b]=a
        idx=[i for i in range(2*N-1) if A[i]==-1]
        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])

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 10700kb

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
Dangerous Syscalls

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

result: