QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#716102#9570. Binary TreeSulfox#WA 24ms3960kbC++202.3kb2024-11-06 14:20:332024-11-06 14:20:33

Judging History

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

  • [2024-11-06 14:20:33]
  • 评测
  • 测评结果:WA
  • 用时:24ms
  • 内存:3960kb
  • [2024-11-06 14:20:33]
  • 提交

answer

#include <bits/stdc++.h>

const int N = 1e5 + 5;

int n, rt, cr, siz[N], hs[N], son[N][2];
bool isr[N];

void find1(int u) {
    siz[u] = 1;
    hs[u] = 0;
    if (son[u][0]) {
        find1(son[u][0]);
        siz[u] += siz[son[u][0]];
        hs[u] = std::max(hs[u], siz[son[u][0]]);
    }
    if (son[u][1]) {
        find1(son[u][1]);
        siz[u] += siz[son[u][1]];
        hs[u] = std::max(hs[u], siz[son[u][1]]);
    }
}
void find2(int u) {
    hs[u] = std::max(hs[u], siz[rt] - siz[u]);
    if (son[u][0]) {
        find2(son[u][0]);
    }
    if (son[u][1]) {
        find2(son[u][1]);
    }
    if (!son[u][0] && !son[u][1]) return;
    if (cr == -1 || hs[cr] > hs[u]) cr = u;
}

void run() {
    scanf("%d", &n);
    for (int u = 1; u <= n; u++)
        isr[u] = true;
    for (int u = 1; u <= n; u++) {
        scanf("%d%d", &son[u][0], &son[u][1]);
        isr[son[u][0]] = isr[son[u][1]] = false;
    }
    for (int u = 1; u <= n; u++)
        if (isr[u])
            rt = u;
    for (; ; ) {
        cr = -1;
        find1(rt);
        find2(rt);
        if (siz[rt] == 1) {
            printf("! %d\n", rt);
            fflush(stdout);
            break;
        }
        int u = rt;
        if (!son[u][0]) {
            printf("? %d %d\n", u, son[u][1]);
            fflush(stdout);
            int res;
            scanf("%d", &res);
            if (res == 0) {
                son[u][1] = 0;
            } else {
                rt = son[u][1];
            }
        } else if (!son[u][1]) {
            printf("? %d %d\n", u, son[u][0]);
            fflush(stdout);
            int res;
            scanf("%d", &res);
            if (res == 0) {
                son[u][0] = 0;
            } else {
                rt = son[u][0];
            }
        } else {
            printf("? %d %d\n", son[u][0], son[u][1]);
            fflush(stdout);
            int res;
            scanf("%d", &res);
            if (res == 0) {
                rt = son[u][0];
            } else if (res == 1) {
                son[u][0] = son[u][1] = 0;
            } else {
                rt = son[u][1];
            }
        }
    }
}

int main() {
    int T;
    scanf("%d", &T);
    for (; T; T--) run();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3884kb

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
Wrong Answer
time: 24ms
memory: 3960kb

input:

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

output:

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

result:

wrong answer Too many queries (test case 1)