QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#738063#9570. Binary TreewdwWA 1ms3844kbC++202.7kb2024-11-12 17:37:302024-11-12 17:37:34

Judging History

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

  • [2024-11-12 17:37:34]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3844kb
  • [2024-11-12 17:37:30]
  • 提交

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> ma, vis, siz;
int ans1 = 0, ans2 = 0;
int id1, id2;
int num = 0;

void dfs(int u, int fa) {
    num++;
    siz[u] = 1;
    ma[u] = 0;
    for (auto j: q[u]) {
        if (j == fa || vis[j])continue;
        dfs(j, u);
        siz[u] += siz[j];
        ma[u] = max(siz[j], ma[u]);
    }
}

void dfs2(int u, int fa) {
    ma[u] = max(ma[u], num - siz[u]);
    for (auto j: q[u]) {
        if (j == fa || vis[j])continue;
        dfs2(j, u);
    }
    if (ans1 > ma[u]) {
        ans1 = ma[u];
        id1 = u;
    }

}

void solve() {
    int n;
    cin >> n;
    siz = vis = ma = 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;
    ans1 = 1e9;
    dfs(1, 0);
    dfs2(1, 0);
    int cnt = 0;
    int fa1 = id1;
    for (int i = 0; i < q[id1].size(); i++) {
        int y = q[id1][i];
        if (vis[y])continue;
        cnt++;
        if (cnt == 1) {
            id2 = y;
        } else if (cnt == 2) {
            id1 = y;
        }
    }
    int q1 = js(id1, id2);

    while (1) {
        if (q1 == 0) {
            if (cnt >=2) {
                vis[fa1] = 1;
            } else if (cnt == 1) {
                vis[id2] = 1;
            }
        } else if (q1 == 2) {
            if (cnt >= 2) {
                vis[fa1] = 1;
            } else if (cnt == 1) {
                vis[id1] = 1;
            }
            id1 = id2;
        } else {
            vis[id1] = 1, vis[id2] = 1;
            id1 = fa1;
        }
        num = 0;
        ans1 = 1e9;
        dfs(id1, 0);
        if (num == 1) {
            cout << "!" << " " << id1 << endl;
            return;
        }
        dfs2(id1, 0);
        cnt = 0;
        fa1 = id1;
        for (int i = 0; i < q[id1].size(); i++) {
            int y = q[id1][i];
            if (vis[y])continue;
            cnt++;
            if (cnt == 1) {
                id2 = y;
            } else if (cnt == 2) {
                id1 = y;
            }
        }
        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: 3844kb

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3664kb

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
2
2
0
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
0
1
2
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
2
2
2
5
4 5
3 1
0 0
0 0
0 0
1
0
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
1
2
5
3 0
5 1
0 0
0 0
4 0
0
0
5
5 0
0 0
0 0
3 0
2 4
1
2
3
3 0
1 0
0 0
2
2
2 0
0 0
2
3
2 3
0 0
0 0
2
10
2 8
9 7
0 0
...

output:

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

result:

wrong answer Too many queries (test case 90)