QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#729610#9570. Binary Treeucup-team3215#RE 0ms0kbC++232.3kb2024-11-09 17:28:252024-11-09 17:28:50

Judging History

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

  • [2024-11-09 17:28:50]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-09 17:28:25]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

int ask(int u, int v) {
    cout << u + 1 << " " << v + 1 << endl;
    int res;
    cin >> res;
    return res;
}

void answer(int v) {
    cout << "! " << v + 1 << endl;
}

vector<int> sz;
vector<char> used;
vector<vector<int>> g;

void dfs(int v, int p) {
    sz[v] = 1;
    for (auto to: g[v]) {
        if (to == p || used[to])continue;
        dfs(to, v);
        sz[v] += sz[to];
    }
}

int find(int v, int p, int S) {
    int mx = S - sz[v];
    for (auto to: g[v]) {
        if (to == p || used[to])continue;
        int it = find(to, v, S);
        if (~it)return it;
        mx = max(mx, sz[to]);
    }
    return mx <= S / 2 ? v : -1;
}

void go(int v) {
    dfs(v, -1);
    int S = sz[v];
    if (S == 1) {
        answer(v);
        return;
    }
    if (S == 2) {
        int other = -1;
        for (auto to: g[v]) if (!used[to])other = to;
        int it = ask(v, other);
        if (!it)answer(v);
        else answer(other);
        return;
    }
    v = find(v, -1, S);
    vector<array<int, 2>> all;
    for (auto to: g[v]) {
        if (used[to])continue;
        all.push_back({sz[to], to});
    }
    sort(all.begin(), all.end());
    int t0 = all.size() < 3 ? -1 : all[0][1];
    int t1, t2;
    for (auto to: g[v]) {
        if (used[to])continue;
        if (to == t0)continue;
        t1 = to;
    }
    for (auto to: g[v]) {
        if (used[to])continue;
        if (to == t0 || to == t1)continue;
        t2 = to;
    }
    int z = ask(t1, t2);
    if (!z) {
        used[v] = 1;
        go(t1);
    } else if (z == 2) {
        used[v] = 1;
        go(t2);
    } else {
        used[t1]=used[t2]=1;
        go(v);
    }
}

void solve() {
    g.clear();
    used.clear();
    sz.clear();
    int n;
    cin >> n;
    g.resize(n);
    used.resize(n);
    sz.resize(n);
    for (int i = 0; i < n; ++i) {
        int x, y;
        cin >> x >> y;
        --x, --y;
        if (~x) {
            g[i].push_back(x);
            g[x].push_back(i);
        }
        if (~y) {
            g[i].push_back(y);
            g[y].push_back(i);
        }
    }
    go(0);
}

int main() {
    int t;
    cin >> t;
    while (t--)solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

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

output:

3 1

result: