QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#793299#9570. Binary TreeSunwkingRE 1ms3532kbC++203.3kb2024-11-29 18:24:492024-11-29 18:24:50

Judging History

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

  • [2024-11-29 18:24:50]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3532kb
  • [2024-11-29 18:24:49]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using LL = long long;
using PII = pair<int , int>;

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

    vector<vector<int>> g(n + 1);

    int nn = n;
    auto add = [&](int a , int b){
        g[a].push_back(b);
        g[b].push_back(a);
    };

    auto ask = [&](int u , int v){
        cout << "? " << u << " " << v << endl;
        cout.flush();

        int x;
        cin >> x;
        return x;
    };

    for(int i = 1 ; i <= n ; i ++ ){
        int x , y;
        cin >> x >> y;
        if(x) add(i , x);
        if(y) add(i , y);
    }

    auto find = [&](int x){
        vector<int> cnt(nn + 1) , sum(nn + 1) , st(nn + 1);
        int mx = 1e9 , k = 0;
        auto dfs = [&](auto f , int u) -> int {
            st[u] = true;

            for(int v : g[u]){
                if(st[v]) continue;
                int s = f(f , v);
                sum[u] += s;
                cnt[u] = max(cnt[u] , s);
            }

            cnt[u] = max(cnt[u] , n - sum[u] - 1);
            if(mx > cnt[u]){
                mx = cnt[u];
                k = u;
            }
            return sum[u] + 1;
        };

        dfs(dfs , x);

        return k;
    };

    int last = 1;
    while(1){
        int x = find(last);

//        cout << x << endl;
        if(g[x].size() == 0){
            cout << "! " << x << endl;
            cout.flush();
            return ;
        }

        if(g[x].size() == 1){
            int t = ask(x , g[x][0]);
            int ans;
            if(t == 0) ans = x;
            else ans = g[x][0];
            cout << "! " << ans << endl;
            cout.flush();
            return ;
        }

        if(g[x].size() == 2){
            auto a = g[x];
            int t = ask(a[0] , a[1]);
            g[x].clear();

            if(t == 0){
                last = a[0];
                g[a[0]].erase(std::find(g[a[0]].begin(), g[a[0]].end(),x));
            }else if(t == 1){
                cout << "! " << x << endl;
                cout.flush();
                return ;
            }
            else last = a[1],g[a[1]].erase(std::find(g[a[1]].begin(), g[a[1]].end(),x));
        }

        if(g[x].size() == 3){
            auto a = g[x];
            int t = ask(a[0] , a[1]);
            g[x].clear();
            if(t == 0) {
                last = a[0];
                g[a[0]].erase(std::find(g[a[0]].begin(), g[a[0]].end(),x));
            }else if(t == 1){
                last = x;
                g[x].push_back(a[2]);
            }else { last = a[1];
                g[a[1]].erase(std::find(g[a[1]].begin(), g[a[1]].end(),x));
            }
        }

        vector<int> st(nn + 1);
        n = 0;
        auto dfs = [&](auto f , int u) -> void{
            st[u] = true;
            n ++;

            for(int x : g[u]){
                if(st[x]) continue;
                f(f , x);
            }
        };

        dfs(dfs , last);

//        for(int i = 1 ; i <= nn ; i ++ )
//            for(int x : g[i])
//                cout << i << " " << x << endl;
    }

}

int main(){

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    cin >> t;

    while(t -- )
        solved();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
? 2 1
! 1

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

output:

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

result: