QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#733416#9570. Binary Treeqinglu09WA 3ms3868kbC++233.5kb2024-11-10 18:43:552024-11-10 18:43:56

Judging History

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

  • [2024-11-10 18:43:56]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3868kb
  • [2024-11-10 18:43:55]
  • 提交

answer

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

using namespace std;

constexpr int inf = 1E18;
int num=0;
int ask (int x, int y) {
    cout << "? " << x << " " << y << endl;

    int t;
    cin >> t;

    return t;
}

void Print (int x) {
    
    cout << "! " << x << endl;
    if(num==90)
    {
        cout << x << endl;
        exit(0);
    }
    
}

void solve () {
    ++num;
    
    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;
            }
        }
        sz.assign (n + 1, 1);

        vector <int> b;
        for (auto y : e[cent]) {
            if (! ok[y]) {
                b.push_back (y);
            }
        }
        if (b.size () == 3) {
            for (int i = 0; i < 3; i ++) {
                dfs (dfs, b[i], cent);
            }
            sort (b.begin (), b.end (),
                [&] (const int &x, const int &y) {
                    return sz[x] > sz[y];
                }
            );
            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);
    num++;
    int T = 1;
    cin >> T;

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

    return 0;
}

詳細信息

Test #1:

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

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 3564kb

input:

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

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

result:

wrong answer Token parameter [name=op] equals to "3", doesn't correspond to pattern "[?!]{1,1}" (test case 90)