QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#727825#9570. Binary Treeucup-team112#RE 7ms10624kbPython31.7kb2024-11-09 13:55:042024-11-09 13:55:04

Judging History

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

  • [2024-11-09 13:55:04]
  • 评测
  • 测评结果:RE
  • 用时:7ms
  • 内存:10624kb
  • [2024-11-09 13:55:04]
  • 提交

answer


def query(u,v):
    print ("?" , u + 1 , v + 1,flush=True)
    cat = int(input())
    return cat

def line_search(b,a):

    line = []

    v = a
    while len(line) == 0 or line[-1] != b:
        line.append(v)
        v = p[v]

    line.reverse()
    # [b , ... , a]

    lidx = 0
    ridx = len(line)-1

    while lidx != ridx:
        assert lidx < ridx
        l = line[lidx]
        r = line[ridx]
        cat = query( l,r )


        midx_l = (lidx + ridx) // 2
        midx_r = (lidx + ridx + 1) // 2

        if cat == 0:
            ridx = midx_l
        elif cat == 1:
            lidx = ridx = midx_l
        else:
            lidx = midx_r

    return line[lidx]

TT = int(input())

for loop in range(TT):

    n = int(input())

    lis = [ [] for i in range(n) ]
    p = [None] * n

    for i in range(n):

        x,y = map(int,input().split())
        x -= 1
        y -= 1

        if x != -1:
            lis[i].append(x)
            p[x] = i
        if y != -1:
            lis[i].append(y)
            p[y] = i


    root = None
    for i in range(n):
        if p[i] == None:
            root = i
            break

    # search
    a = root
    b = root #直線探索用

    while True:
        
        if len(lis[a]) == 0:
            ans = line_search(b,a)
            break
        elif len(lis[a]) == 1:
            a = lis[a][0]
        else: #2
            u,v = lis[a][0] , lis[a][1]
            cat = query(u,v)

            if cat == 0:
                a = b = u
            elif cat == 1:
                ans = line_search(b,a)
                break
            else:
                a = b = v

    print ("!",ans+1,flush=True)
            

详细

Test #1:

score: 100
Accepted
time: 7ms
memory: 10624kb

input:

2
5
0 0
1 5
2 4
0 0
0 0
0
1
2
0 2
0 0
2

output:

? 2 4
? 1 5
! 2
? 1 2
! 2

result:

ok OK (2 test cases)

Test #2:

score: -100
Dangerous Syscalls

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
0
2
0
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
0
1
1
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
0
2
1

output:

? 8 6
? 5 4
? 4 3
! 4
? 7 8
? 1 4
? 7 2
! 3
? 1 7
? 5 8
? 4 2
? 8 6

result: