QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#724591 | #9570. Binary Tree | 552Hz | RE | 1ms | 3624kb | C++23 | 2.1kb | 2024-11-08 13:57:43 | 2024-11-08 13:57:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin>>n;
vector<int>e[n+1];
for(int i =1;i<=n;i++)
{
int a,b;
cin>>a>>b;
if(a != 0)
{
e[i].push_back(a);
e[a].push_back(i);
}
if(b != 0)
{
e[i].push_back(b);
e[b].push_back(i);
}
}
auto ask = [&](int u,int v)
{
cout<<"? "<<u<<" "<<v<<endl;
int res;
cin>>res;
return res;
};
auto output = [&](int ans)
{
cout<<"! "<<ans<<endl;
};
vector<int>sz(n+1,1),del(n+1);
int pos = 0,cur = 0;
function<void(int,int)>dfs = [&](int u,int fa)
{
int res = 0;
for(auto v:e[u])
{
if(v == fa || del[v])
continue;
dfs(v,u);
sz[u] += sz[v];
res = max(res,sz[v]);
}
res = max(res,cur - sz[u]);
if(res <= cur / 2)
pos = u;
};
function<void(int,int)>det = [&](int u,int fa)
{
del[u] = 1;
for(auto v:e[u])
{
if(v == fa || del[v])
continue;
det(v,u);
}
};
for(int query_times = -1;(1 << query_times) <= n;query_times ++)
{
cur = count(del.begin()+1,del.end(),0);
dfs(find(del.begin()+1,del.end(),0) - del.begin(),0);
vector<int>son;
for(auto v:e[pos])
{
if(!del[v])
son.push_back(v);
}
if(son.size() == 3)
{
int op = ask(son[0],son[1]);
if(op == 0)
{
det(son[1],pos);
det(son[2],pos);
del[pos] = 1;
}
if(op == 1)
{
det(son[0],pos);
det(son[1],pos);
}
if(op == 2)
{
det(son[0],pos);
det(son[2],pos);
del[pos] = 1;
}
}
if(son.size() == 2)
{
int op = ask(son[0],son[1]);
del[pos] = 1;
if(op == 0)
det(son[1],pos);
if(op == 2)
det(son[0],pos);
if(op == 1)
return void(output(pos));
}
if(son.size() == 1)
{
int op = ask(son[0],pos);
if(op == 0)
return void(output(son[0]));
if(op == 2)
return void(output(pos));
}
if(son.empty())
return void(output(pos));
}
output(1);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3624kb
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 2 2 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 0 2 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 2 0
output:
? 1 8 ? 5 4 ? 3 4 ! 3 ? 2 7 ? 5 6 ? 5 7 ! 7 ? 5 8 ? 6 8 ! 6