QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#730172#9570. Binary Treeucup-team3924#RE 1ms3540kbC++202.2kb2024-11-09 19:01:582024-11-09 19:01:59

Judging History

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

  • [2024-11-09 19:01:59]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3540kb
  • [2024-11-09 19:01:58]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;

void printans(int x){
    cout << "! " << x + 1 << endl;
}

int query(int a, int b){
    cout << "? " << a + 1 << " " << b + 1 << endl;
    int ans;
    cin >> ans;
    return ans;
}

void dfs(int head, int d, vector<vector<int>> &tree, vector<int> &dist, set<int> &in){
    dist[head] = d;
    for(int i : tree[head]){
        if(!dist[i] && in.count(i)){
            dfs(i, d + 1, tree, dist, in);
        }
    }
}

void rec(int N, vector<vector<int>> tree, set<int> in){
    assert(in.size());
    if(in.size() == 1){
        printans(*in.begin());
        return;
    }
    vector<int> d(N), d1(N), d2(N);
    dfs(*in.begin(), 1, tree, d, in);
    int a = max_element(d.begin(), d.end()) - d.begin();
    dfs(a, 1, tree, d1, in);
    int b = max_element(d1.begin(), d1.end()) - d1.begin();
    dfs(b, 1, tree, d2, in);
    // if(d1[b] % 2 == 0 && in.size() > 2){
    //     int dst = d1[b];
    //     for(int i : in){
    //         if(d1[i] + 1 == dst){
    //             b = i;
    //             fill(d2.begin(), d2.end(), 0ll);
    //             dfs(i, 1, tree, d2, in);
    //             break;
    //         }
    //     }
    // }
    int r = query(a, b);
    set<int> nin;
    if(r == 1){
        for(int i : in){
            if(d1[i] == d2[i]){
                nin.insert(i);
            }
        }
    }
    else if(r == 0){
        for(int i : in){
            if(d1[i] < d2[i]){
                nin.insert(i);
            }
        }
    }
    else{
        for(int i : in){
            if(d1[i] > d2[i]){
                nin.insert(i);
            }
        }
    }
    rec(N, tree, nin);
}

void solve(){
    int N;
    cin >> N;
    vector<vector<int>> tree(N);
    set<int> in;
    for(int i = 0; i < N; i ++){
        int a, b;
        in.insert(i);
        cin >> a >> b;
        if(a){
            tree[i].push_back(a - 1);
            tree[a - 1].push_back(i);
        }
        if(b){
            tree[i].push_back(b - 1);
            tree[b - 1].push_back(i);
        }
    }
    rec(N, tree, in);
}

signed main(){
	int t = 1;
	cin >> t;
	while(t--)solve();
	return 0;
}

详细

Test #1:

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

input:

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

output:

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

result: