QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#730145#9570. Binary Treeucup-team3160#TL 0ms14044kbC++142.7kb2024-11-09 18:51:582024-11-09 18:51:58

Judging History

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

  • [2024-11-09 18:51:58]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:14044kb
  • [2024-11-09 18:51:58]
  • 提交

answer

#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include "lib.h"
#endif
#define rep(i, min, max) for(int i = (min); i <= (max); ++i)
#define nrep(i, max, min) for(int i = (max); i >= (min); --i)
#define reads(str) (scanf("%s", str + 1), strlen(str + 1))
#define case() int Ts = read(); rep(T, 1, Ts)
#define putf(flag) puts((flag) ? "Yes" : "No")
#define put(x) printf("%d ", x)
#define putl(x) printf("%lld ", x)
#define endl() putchar('\n')
using namespace std;

typedef long long ll;
inline int read()
{
    int now=0; bool nev=false; char c=getchar();
    while(c<'0' || c>'9') { if(c=='-') nev=true; c=getchar(); }
    while(c>='0' && c<='9') { now=(now<<1)+(now<<3)+(c&15); c=getchar(); }
    return nev?-now:now;
}

const int N = 1e6 + 10;

int n;
int ls[N], rs[N], vis[N], fa[N];
int siz[N];
int deg[N];

int ask(int x, int y) {
    printf("? %d %d\n", x, y); fflush(stdout);
    return read();
}

int p = 0, mn = 1e9;
void dfs(int x) {
    if(vis[x] || !x) { siz[x] = 0; return; }
    int l = ls[x], r = rs[x];
    dfs(l), dfs(r);
    siz[x] = 1 + siz[l] + siz[r];
    int cur = max({siz[l], siz[r], n - siz[x] + 1});
    if(cur <= mn) p = x, mn = cur;
}

int main() {
    case() {
        n = read();
        rep(i, 1, n) ls[i] = read(), rs[i] = read();
        rep(i, 0, n) deg[i] = 0, vis[i] = 0;
        rep(i, 1, n) deg[ls[i]] ++, deg[rs[i]] ++, fa[ls[i]] = i, fa[rs[i]] = i;
        int rt = 0;
        rep(i, 1, n) if(deg[i] == 0) rt = i;
        int last = 1e9;
        // ok();
        while(1) {
            p = 0, mn = 1e9;
            dfs(rt);
            // print(rt, siz[rt]);
            fflush(stdout);
            // assert(siz[rt] <= last / 2);
            if(siz[rt] == 1) {
                printf("! %d\n", rt); fflush(stdout);
                break;
            }
            int l = ls[p], r = rs[p];
            // print(rt, p, l, r, mn);
            if(l != 0 && r != 0) {
                int op = ask(l, r);
                if(op == 0) rt = l;
                if(op == 2) rt = r;
                if(op == 1) vis[l] = vis[r] = 1;
            } else {
                // assert(l || r);
                int son = l | r, fa = ::fa[p];
                if(mn <= siz[rt] / 2) {
                    int op = ask(p, son);
                    if(op == 0) vis[son] = 1;
                    if(op == 2) rt = son;
                } else {
                    // assert(siz[p] == siz[rt] / 2 + 1);
                    int op = ask(fa, son);
                    if(op == 0) vis[p] = 1;
                    if(op == 2) rt = son;
                    if(op == 1) vis[son] = 1, rt = p;
                }
            }
            last = siz[rt];
        }
    }
}

详细

Test #1:

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

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
Time Limit Exceeded

input:

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

output:

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

result: