QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#724358 | #9570. Binary Tree | 552Hz | RE | 0ms | 3628kb | C++23 | 2.5kb | 2024-11-08 12:26:49 | 2024-11-08 12:26:49 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
void solve(){
int cntq=0;
int n;
cin>>n;
vector<vector<int>> adj(n+1);
for(int i=1;i<=n;i++){
int x,y;
cin>>x>>y;
if(x){
adj[i].push_back(x);
adj[x].push_back(i);
}
if(y){
adj[i].push_back(y);
adj[y].push_back(i);
}
}
vector<int> del(n+1);
vector<int> zx;
vector<int> siz(n+1);
int cn=n;
vector<int> csiz(n+1);
auto get=[&](auto &&self,int u,int la)->void{
siz[u]=1;
int res=0;
int c=0;
vector<int> cur;
for(auto x:adj[u]){
if(x==la||del[x])
continue;
self(self,x,u);
siz[u]+=siz[x];
res=max(res,siz[x]);
cur.push_back(x);
c++;
}
if(cn-siz[u]>0){
c++;
cur.push_back(la);
}
res=max(res,cn-siz[u]);
if(res<=cn/2){
if(c==3&&zx.empty()){
csiz[cur[0]]=siz[cur[0]];
csiz[cur[1]]=siz[cur[1]];
csiz[cur[2]]=cn-siz[u];
}
zx.push_back(u);
}
};
auto de=[&](auto &&self,int u,int la)->void{
del[u]=1;
cn--;
for(auto x:adj[u]){
if(x==la||del[x])
continue;
self(self,x,u);
}
};
int id=1;
while(1){
zx.clear();
while(id<=n&&del[id])
id++;
if(cn == 1)
return void(cout<<"! "<<id<<endl);
get(get,id,0);
int z=zx[0];
int cnt=0;
vector<int> cur;
for(auto x:adj[z]){
if(!del[x]){
cnt++;
cur.push_back(x);
}
}
if(cnt==3){
cntq++;
assert((1<<cntq)<=n);
sort(cur.begin(),cur.end(),[&](int x,int y){
return csiz[x]>csiz[y];
});
cout<<"? "<<cur[0]<<" "<<cur[1]<<endl;
int g;
cin>>g;
if(g==0){
de(de,cur[1],z);
de(de,cur[2],z);
}
else if(g==1){
de(de,cur[1],z);
de(de,cur[0],z);
}
else{
de(de,cur[0],z);
de(de,cur[2],z);
}
if(g != 1)
{
cn--;
del[z] = 1;
}
}
else if(cnt==2){
cntq++;
assert((1<<cntq)<=n);
cout<<"? "<<cur[0]<<" "<<cur[1]<<endl;
int g;
cin>>g;
if(g==0){
de(de,cur[1],z);
}
else if(g==1){
cout<<"! "<<z<<endl;
return;
}
else{
de(de,cur[0],z);
}
cn--;
del[z] = 1;
}
else{
cntq++;
assert((1<<cntq)<=n);
cout<<"? "<<cur[0]<<" "<<z<<endl;
int g;
cin>>g;
if(g==0){
cout<<"! "<<cur[0]<<endl;
return;
}
else{
cout<<"! "<<z<<endl;
return;
}
}
}
}
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: 0ms
memory: 3628kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 1 ? 2 5 ! 2 ? 1 2 ! 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 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 0 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 4 ? 2 7 ? 1 2 ! 2 ? 5 3 ? 1 4 ? 2 3 ! 2 ? 1 6 ? 1 7 ? 1 5 ! 5 ? 4 5 ? 3 1 ! 1 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 4 5 ! 5 ? 4 1 ? 3 4 ! 3 ? 3 2 ! 2 ? 1 2 ! 1 ? 2 3 ! 3 ? 2 6 ? 1 9 ? 9 10 ! 10 ? 1 2 ! 1 ? 5 9 ? 4 8 ? 3 5 ! 3 ? 5 8 ? 7 1 ? 3 5 ! 3 ? 9 3 ? 1 7 ? 2 9 ! 2 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 2 3 ? ...