QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#729082 | #9570. Binary Tree | ucup-team3661# | WA | 3ms | 3608kb | C++20 | 1.9kb | 2024-11-09 16:27:57 | 2024-11-09 16:27:59 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int N;
vector<int>G[2<<17];
int seen[2<<17],sz[2<<17];
int cent(int root){
auto dfs=[&](auto&&dfs,int now,int pre)->int{
sz[now]=1;
for(int nxt:G[now]){
if(nxt==pre||seen[nxt])continue;
sz[now]+=dfs(dfs,nxt,now);
}
return sz[now];
};
int total=dfs(dfs,root,-1);
int cent=root;
auto dfs2=[&](auto&&dfs2,int now,int pre)->void{
bool ok=(total-sz[now])*2<=total;
for(int nxt:G[now]){
if(nxt==pre||seen[nxt])continue;
dfs2(dfs2,nxt,now);
if(sz[nxt]*2>total)ok=false;
}
if(ok)cent=now;
};
dfs2(dfs2,root,-1);
return cent;
}
int ask(int u,int v){
cout<<"? "<<++u<<' '<<++v<<endl;
int ret;cin>>ret;
return ret;
}
int main(){
int T;cin>>T;
while(T--){
int N;cin>>N;
for(int i=0;i<N;i++){
G[i].clear();
seen[i]=false;
sz[i]=0;
}
for(int i=0;i<N;i++){
int u,v;cin>>u>>v;
u--;v--;
if(u>=0){
G[i].push_back(u);
G[u].push_back(i);
}
if(v>=0){
G[i].push_back(v);
G[v].push_back(i);
}
}
int ans=-1;
auto solve=[&](auto&&solve,int now)->void{
int ce=cent(now);
seen[ce]=true;
vector<int>ch;
for(int nxt:G[ce]){
if(!seen[nxt])ch.push_back(nxt);
}
int deg=ch.size();
if(deg==0){
ans=ce;
}else if(deg==1){
int res=ask(ce,ch.front());
if(res==0){
seen[ch.front()]=true;
solve(solve,ce);
}else if(res==2){
solve(solve,ch.front());
}
}else if(deg==2){
int le=ch[0],ri=ch[1];
int res=ask(le,ri);
if(res==0)solve(solve,le);
else if(res==1)ans=ce;
else solve(solve,ri);
}else if(deg==3){
int res=ask(ch[0],ch[1]);
if(res==1){
seen[ce]=false;
seen[ch[0]]=true;
seen[ch[1]]=true;
solve(solve,ce);
}
else if(res==0) solve(solve,ch[0]);
else solve(solve,ch[1]);
}
};
solve(solve,0);
cout<<"! "<<++ans<<endl;
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3608kb
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 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 3ms
memory: 3576kb
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 2 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 2 1 2 5 4 5 3 1 0 0 0 0 0 0 1 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 2 5 3 0 5 1 0 0 0 0 4 0 0 0 5 5 0 0 0 0 0 3 0 2 4 1 2 3 3 0 1 0 0 0 2 2 2 0 0 0 2 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 1 8 ? 5 4 ? 4 3 ! 4 ? 2 7 ? 7 8 ? 8 6 ! 8 ? 5 8 ? 4 2 ? 6 8 ! 8 ? 4 5 ? 3 1 ! 3 ? 5 6 ? 1 4 ! 4 ? 5 1 ? 5 4 ! 5 ? 1 2 ? 3 5 ! 5 ? 3 2 ! 2 ? 1 2 ! 2 ? 2 3 ! 3 ? 1 9 ? 2 6 ? 4 3 ! 4 ? 1 2 ! 1 ? 5 9 ? 2 1 ? 2 6 ! 2 ? 3 10 ? 6 2 ? 4 10 ! 10 ? 3 4 ? 1 7 ? 8 2 ! 2 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 8 4 ?...
result:
wrong answer Too many queries (test case 90)