QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#729544 | #9570. Binary Tree | ucup-team3924# | RE | 1ms | 3480kb | C++20 | 2.3kb | 2024-11-09 17:19:08 | 2024-11-09 17:19:14 |
Judging History
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);
// cerr << "AB " << a << " " << b << " " << *max_element(d.begin(), d.end()) << " " << *max_element(d1.begin(), d1.end()) << endl;
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: 3480kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 2 2 0 2 0 0 0
output:
? 4 2 ? 5 1 ! 1 ? 2 1 ! 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 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 2 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 0 3 2 3 0 0 0 0 0 10 2 8 9 7 0 0 ...
output:
? 3 1 ? 7 2 ? 2 1 ! 1 ? 6 1 ? 3 1 ? 4 2 ! 4 ? 2 3 ? 7 1 ? 5 1 ! 5 ? 3 1 ? 4 5 ! 4 ? 2 1 ? 4 1 ! 4 ? 4 3 ? 3 1 ! 1 ? 3 5 ? 2 1 ! 1 ? 2 3 ! 3 ? 2 1 ! 2 ? 2 3 ! 2 ? 3 1 ? 10 8 ? 8 1 ! 8 ? 2 1 ! 2 ? 4 6 ? 6 9 ? 9 1 ! 1 ? 2 1 ? 9 3 ? 5 1 ! 1 ? 5 2 ? 7 1 ? 9 2 ! 9 ? 2 1 ! 2 ? 2 1 ? 7 1 ! 1 ? 3 1 ? 7 1 ? 7...