QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#755721#9570. Binary TreeMagical_Kingdom#WA 2ms9688kbC++173.2kb2024-11-16 17:56:322024-11-16 17:56:32

Judging History

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

  • [2024-11-16 17:56:32]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:9688kb
  • [2024-11-16 17:56:32]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
int n;
vector<int> to[maxn];
bool del[maxn];
void dfs(int now, int fa, int dep, int& max_dep, int& v) {
    if (dep > max_dep) {
        max_dep = dep;
        v = now;
    }
    for (auto x : to[now]) {
        if (del[x] == true) {
            continue;
        }
        if (x == fa) {
            continue;
        }
        dfs(x, now, dep + 1, max_dep, v);
    }
}

void get_line(int root, int& u, int& v) {
    int max_dep = 0, w = 0;
    dfs(root, 0, 1, max_dep, w);
    int c = 0;
    max_dep = 0;
    dfs(w, 0, 1, max_dep, c);
    u = w, v = c;
}

int dis1[maxn], dis2[maxn];
bool vis[maxn];
void bfs(int start, int dis[]) {
    memset(vis, 0, sizeof(vis));
    // memset(dis, 0x7f, sizeof(dis));
    for (int i = 1; i <= n; i++) {
        dis[i] = 2e9;
    }
    dis[start] = 0;
    vis[start] = 1;
    queue<int> q;
    q.push(start);
    while (!q.empty()) {
        int x = q.front();
        q.pop();

        for (auto y : to[x]) {
            if (del[y] == true || vis[y]) {
                continue;
            }
            dis[y] = dis[x] + 1;
            vis[y] = true;
            q.push(y);
        }
    }
}

void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int x, y;
        cin >> x >> y;
        if (x != 0) {
            to[i].push_back(x);
            to[x].push_back(i);
        }
        if (y != 0) {
            to[i].push_back(y);
            to[y].push_back(i);
        }
    }

    int times = floor(log2(n));

    while (times--) {
        int u = 0, v = 0;
        int root = 0;
        for (int i = 1; i <= n; i++) {
            if (del[i] == false) {
                root = i;
                get_line(root, u, v);
                break;
            }
        }
        cout << "? " << u << ' ' << v << '\n';
        cout.flush();
        int in;
        cin >> in;
        bfs(u, dis1);
        bfs(v, dis2);
        if (in == 0) {
            for (int i = 1; i <= n; i++) {
                if (del[i] == false) {
                    if (dis2[i] <= dis1[i]) {
                        del[i] = true;
                    }
                }
            }
        } else if (in == 1) {
            for (int i = 1; i <= n; i++) {
                if (del[i] == false) {
                    if (dis2[i] != dis1[i]) {
                        del[i] = true;
                    }
                }
            }
        } else {
            for (int i = 1; i <= n; i++) {
                if (del[i] == false) {
                    if (dis2[i] >= dis1[i]) {
                        del[i] = true;
                    }
                }
            }
        }
    }

    int cnt = 0;
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        if (del[i] == false) {
            cnt++;
            ans = i;
        }
    }
    if (cnt > 1 || cnt <= 0) {
        assert(1);
    }
    cout << "! " << ans << '\n';
    cout.flush();
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 9688kb

input:

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

output:

? 4 1
? 5 1
! 2
? 2 2

result:

wrong answer Query two identical vertices 2 (test case 2)