QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#744479 | #9570. Binary Tree | doyo | WA | 1ms | 3612kb | C++20 | 3.0kb | 2024-11-13 22:03:41 | 2024-11-13 22:03:42 |
Judging History
answer
#include <bits/stdc++.h>
#define N 2000001
using namespace std;
int work(){
int i, n; cin >> n;
vector<int> lc(n + 1);
vector<int> rc(n + 1);
vector<int> fa(n + 1);
vector<vector<int> > to(n + 1);
for(i=1;i <= n;i++){
cin >> lc[i] >> rc[i];
fa[lc[i]] = i;
fa[rc[i]] = i;
if(lc[i]) {
to[i].push_back(lc[i]);
to[lc[i]].push_back(i);
}
if(rc[i]) {
to[i].push_back(rc[i]);
to[rc[i]].push_back(i);
}
}
int rt = 0, Ans;
vector<int> siz(n + 1);
vector<int> mx(n + 1);
vector<bool> vis(n + 1);
auto root =[&](auto self, int u, int f, int total) -> void{
siz[u] = 1; mx[u] = 0;
for(auto v:to[u]){
if(v == f || vis[v]) continue;
self(self, v, u, total);
siz[u] += siz[v];
mx[u] = max(mx[u],siz[v]);
}
mx[u] = max(mx[u],total - siz[u]);
//cout << u << "-" << mx[u] << endl;
if(!rt || mx[u] < mx[rt]) rt = u;
};
auto dfs =[&] (auto self, int u) -> void{
vis[u] = 1;
//cout <<"dfs" << u <<endl;
int num = 0;
for(auto v:to[u]) if(!vis[v]) num++;
if(num == 3){
cout <<"? " << lc[u] << " " << rc[u] <<endl;
int t, v; cin >> t;
if(t == 0) v = lc[u];
if(t == 2) v = rc[u];
if(t == 1) {
cout <<"? " << fa[u] << " " << lc[u] <<endl;
int tt; cin >> tt;
if(tt == 1){
Ans = u;
return;
}
v = fa[u];
}
rt = 0; root(root, v, 0, siz[v]);
self(self, rt);
}
if(num == 2){
int x, y; x = y = 0 ;
for(auto v:to[u])
if(!vis[v])
if(!x) x = v;
else y = v;
cout <<"? " << x << " " << y <<endl;
int t, v; cin >> t;
if(t == 0) v = x;
if(t == 2) v = y;
if(t == 1){
Ans = u;
return;
}
rt = 0; root(root, v, 0, siz[v]);
self(self, rt);
}
if(num == 1){
int x = 0;
for(auto v:to[u])
if(!vis[v]) x = v;
cout <<"? " << u << " " << x <<endl;
int t, v; cin >> t;
if(t == 0){
Ans = u;
return;
}
if(t == 2){
Ans = x;
return;
}
cout <<"Error\n";
return;
}
if(num == 0){
Ans = u;
return;
}
};
mx[0]=n; root(root, 1, 0, n); dfs(dfs, rt);
cout <<"! " << Ans <<endl;
return 0;
}
int main()
{
std::cin.tie(0);
std::cin.sync_with_stdio(0);
int t=1;
std::cin>>t;
while(t--)
work();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3612kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0
output:
? 1 5 ? 3 1 ? 4 3
result:
wrong answer Too many queries (test case 1)