QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#740246#9570. Binary TreefosovRE 0ms0kbC++143.2kb2024-11-13 06:56:452024-11-13 06:56:45

Judging History

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

  • [2024-11-13 06:56:45]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-13 06:56:45]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;

#define ll long long 
#define INF 0x3f3f3f3f
#define LNF 0x3f3f3f3f3f3f3f3fll
#define MOD 998244353
#define pii pair<int, int>

#define N 100010
#define K 20

int l[N], r[N], vld[N], f[N], sz[N], ind[N];

void dfs(int u) {
    if (!vld[u]) return; 
    sz[u] = 1;
    dfs(l[u]);
    dfs(r[u]);
    sz[u] += sz[l[u]] + sz[r[u]];
}

void fg(int u, int flag) {
    if (!vld[u]) return;
    f[u] = flag;
    fg(l[u], flag);
    fg(r[u], flag);
}

int root(int n) {
    for (int i = 1; i <= n; ++ i) ind[i] = 0;
    for (int i = 1; i <= n; ++ i) {
        if (vld[i] && vld[l[i]]) ind[l[i]] ++;
        if (vld[i] && vld[r[i]]) ind[r[i]] ++;
    }
    for (int i = 1; i <= n; ++ i) {
        if (vld[i] && ind[i] == 0) return i;
    }
    return -1;
}

int query(int u, int v) {
    cout << "? " << u << ' ' << v << '\n';
    cout.flush();
    int res; cin >> res;

    if (res == -1) {
        cout << "ask " << u << ' ' << v << '\n';
        exit(1);
    }
    return res;
}

int main() {
#ifdef TEST
    freopen("zz.in", "r+", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t; cin >> t;
    while (t --) {
        int n; cin >> n;
        for (int i = 1; i <= n; ++ i) cin >> l[i] >> r[i];
        for (int i = 1; i <= n; ++ i) vld[i] = 1;

        int rt = root(n);
        for (int i = 1; i <= n; ++ i) sz[i] = 0;
        dfs(rt);
        while (sz[rt] > 1) {
            int mn = INF, pt = -1;
            for (int i = 1; i <= n; ++ i) {
                if (!vld[i]) continue;
                int szl = sz[l[i]];
                int szr = sz[r[i]]; 
                int szu = sz[rt] - szl - szr;

                if ((szl || szr) && max(szl, max(szr, szu)) < mn) {
                    mn = max(szl, max(szr, szu));
                    pt = i;
                }
            }

            int szl = sz[l[pt]];
            int szr = sz[r[pt]]; 
            int szu = sz[rt] - szl - szr;

            assert(max(szl, max(szr, szu)) <= sz[rt] / 2);
            assert(szl || szr);

            fg(rt, 0);
            if (szl && szr) {
                // cout << "@1\n";
                int x = query(l[pt], r[pt]);
                if (x == 0) fg(l[pt], 1);
                if (x == 1) fg(rt, 1), fg(l[pt], 0), fg(r[pt], 0);
                if (x == 2) fg(r[pt], 1);
            }
            if (!szl) {
                // cout << "@2\n";
                int x = query(r[pt], pt);
                if (x == 0) fg(r[pt], 1);
                if (x == 1) assert(1 == 0);
                if (x == 2) fg(rt, 1), fg(r[pt], 0);
            }
            if (!szr) {
                // cout << "@3\n";
                int x = query(l[pt], pt);
                if (x == 0) fg(l[pt], 1);
                if (x == 1) assert(2 == 0);
                if (x == 2) fg(rt, 1), fg(l[pt], 0);
            }

            for (int i = 1; i <= n; ++ i) vld[i] &= f[i];
            
            rt = root(n);
            for (int i = 1; i <= n; ++ i) sz[i] = 0;
            dfs(rt);
        }

        cout << "! " << rt << '\n';
        cout.flush();
    }
}

詳細信息

Test #1:

score: 0
Runtime Error

input:

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

output:


result: