QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#735189#9570. Binary TreewdwTL 1ms3556kbC++203.0kb2024-11-11 18:01:562024-11-11 18:01:56

Judging History

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

  • [2024-11-11 18:01:56]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3556kb
  • [2024-11-11 18:01:56]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
#define int long long
vector<vector<int>> q;
vector<int> fa1;

int js(int a, int b) {
    cout << "? " << a << " " << b << endl;
    int x;
    cin >> x;
    return x;
}

vector<int> dep, vis;
int ans1 = 0, ans2 = 0;
int id1, id2;
int num = 0;

void dfs(int u, int fa) {
    num++;
    for (auto j: q[u]) {
        if (j == fa || vis[j])continue;
        dep[j] = dep[u] + 1;
        if (dep[j] > ans1) {
            ans1 = dep[j];
            id1 = j;
        }
        dfs(j, u);
    }
}


void dfs2(int u, int fa) {
    for (auto j: q[u]) {
        if (j == fa || vis[j])continue;
        fa1[j] = u;
        dep[j] = dep[u] + 1;
        if (dep[j] > ans2) {
            ans2 = dep[j];
            id2 = j;
        }
        dfs2(j, u);
    }
}

void solve() {
    int n;
    cin >> n;
    vis = dep = fa1 = vector<int>(n + 5);
    q = vector<vector<int>>(n + 5);
    for (int i = 1, a, b; i <= n; i++) {
        cin >> a >> b;
        if (a) {
            q[i].push_back(a);
            q[a].push_back(i);
        }
        if (b) {
            q[i].push_back(b);
            q[b].push_back(i);
        }
    }
    ans1 = 0, ans2 = 0;
    num = 0;
    dep[1] = 0;
    dfs(1, 0);
    dep[id1] = 0;
    dfs2(id1, 0);
    int q1 = js(id1, id2);
//    for (int i = 1; i <= n; i++) {
//        cout << i << " " << fa1[i] << " " << fa2[i] << endl;
//    }
//    cout << ans2 << endl;
    while (1) {
        vector<int> px;
        int p1 = id2;
        while (p1 != id1) {
            px.push_back(p1);
            p1 = fa1[p1];
        }
        px.push_back(id1);
        if (q1 == 0) {
            int k1 = ans2;
            int k = px.size() - 1;
            k1 /= 2;
            k1++;
            for (int j = 0; j <= k1 - 1; j++) {
                vis[px[j]] = 1;
            }
            id2 = px[k1];
        } else if (q1 == 2) {
            int k1 = ans2;
            int k = px.size() - 1;
            k1 /= 2;
            k1++;
            for (int j = k; j >= k - k1 + 1; j--) {
                vis[px[j]] = 1;
            }
            id1 = px[k - k1];
        } else {
            int k1 = ans2;
            int k = px.size() - 1;
            k1 /= 2;
            for (int j = 0; j <= k1 - 1; j++) {
                vis[px[j]] = 1;
            }
            id2 = px[k1];
            k1 = ans2;
            k = px.size() - 1;
            k1 /= 2;
            for (int j = k; j >= k - k1 + 1; j--) {
                vis[px[j]] = 1;
            }
            id1 = px[k - k1];
        }
        ans1 = 0, ans2 = 0;
        dep[id1] = 0;
        num = 0;
        dfs(id1, 0);
        if (num == 1) {
            cout << "!" << " " << id1 << endl;
            return;
        }
        dep[id1] = 0;
        dfs2(id1, 0);
        q1 = js(id1, id2);
    }


}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int T = 1;
    cin >> T;
    while (T--) solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3556kb

input:

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

output:

? 4 1
? 1 5
! 2
? 2 1
! 1

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
2
0
2
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
2
1
0
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
2
0
0
5
4 5
3 1
0 0
0 0
0 0
2
0
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
2
0
5
3 0
5 1
0 0
0 0
4 0
2
2
5
5 0
0 0
0 0
3 0
2 4
2
2
3
3 0
1 0
0 0
2
2
2 0
0 0
2
3
2 3
0 0
0 0
0
10
2 8
9 7
0 0
...

output:

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

result: