QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#732765#9570. Binary Tree333zhan#RE 1ms3820kbC++203.1kb2024-11-10 15:49:352024-11-10 15:49:35

Judging History

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

  • [2024-11-10 15:49:35]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3820kb
  • [2024-11-10 15:49:35]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long

using namespace std;

constexpr int inf = 1E18;

int ask (int x, int y) {
    cout << "? " << x << " " << y << endl;

    int t;
    cin >> t;

    return t;
}

void Print (int x) {
    cout << "! " << x << endl;
}

void solve () {
    int n;
    cin >> n;

    vector <vector <int>> e (n + 1);
    for (int i = 1; i <= n; i ++) {
        int x, y;
        cin >> x >> y;

        if (x) {
            e[x].push_back (i);
            e[i].push_back (x);
        }
        if (y) {
            e[y].push_back (i);
            e[i].push_back (y);
        }
    }

    vector <bool> ok (n + 1);

    auto work = [&] (auto &&self, int x, int fa) -> void {
        ok[x] = true;
        for (auto y : e[x]) {
            if (y == fa) {
                continue;
            }
            if (ok[y]) {
                continue;
            }
            self (self, y, x);
        }
    };

    while (true) {
        int rt;
        int m = 0;
        for (int i = 1; i <= n; i ++) {
            if (! ok[i]) {
                rt = i;
                m ++;
            }
        }
        int cent;
        vector <int> sz (n + 1, 1);
        vector <int> wt (n + 1);
        auto dfs = [&] (auto &&self, int x, int fa) -> void {
            for (auto y : e[x]) {
                if (y == fa) {
                    continue;
                }
                if (ok[y]) {
                    continue;
                }
                self (self, y, x);
                sz[x] += sz[y];
                wt[x] = max (wt[x], sz[y]);
            }
            wt[x] = max (wt[x], m - sz[x]);
        };
        dfs (dfs, rt, -1);
        for (int i = 1; i <= n; i ++) {
            if (! ok[i] && wt[i] <= m / 2) {
                cent = i;
                break;
            }
        }
        vector <int> b;
        for (auto y : e[cent]) {
            if (! ok[y]) {
                b.push_back (y);
            }
        }
        if (b.size () == 3) {
            int t = ask (b[0], b[1]);
            if (t == 0) {
                work (work, cent, b[0]);
            } else if (t == 1) {
                work (work, b[0], cent);
                work (work, b[1], cent);
            } else {
                work (work, cent, b[1]);
            }
        } else if (b.size () == 2) {
            int t = ask (b[0], b[1]);
            if (t == 0) {
                work (work, cent, b[0]);
            } else if (t == 1) {
                Print (cent);
                return;
            } else {
                work (work, cent, b[1]);
            }
        } else if (b.size () == 1) {
            int t = ask (cent, b[0]);
            if (t == 0) {
                Print (cent);
            } else {
                Print (b[0]);
            }
            return;
        } else {
            Print (cent);
            return;
        }
    }
}

signed main () {
    ios::sync_with_stdio (false);
    cin.tie (nullptr);

    int T = 1;
    cin >> T;

    while (T --) {
        solve ();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Runtime Error

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
2
2
0
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
2
1
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
0
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:

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

result: