QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#751743 | #9570. Binary Tree | dog207 | WA | 3ms | 9928kb | C++14 | 1.6kb | 2024-11-15 20:25:43 | 2024-11-15 20:25:43 |
Judging History
answer
#include<bits/stdc++.h>
#define mk make_pair
#define X first
#define Y second
#define pb push_back
using namespace std;
const int N=2e5+5;
int n,T,op;
vector<int> G[N];
int vis[N],maxnp,f[N],siz[N],maxn[N];
void dfs(int x,int fa){
f[x]=fa;
siz[x]=1;
maxn[x]=0;
for(int y:G[x]){
if(!vis[y]&&y!=fa){
dfs(y,x);
siz[x]+=siz[y];
maxn[x]=max(maxn[x],siz[y]);
}
}
maxn[x]=max(n-siz[x],maxn[x]);
if(!maxnp||maxn[maxnp]>maxn[x]){
maxnp=x;
}
}
int work(int x){
int d=0;
for(int y:G[x]){
if(!vis[y]){
++d;
}
}
if(d==0){
return x;
}
maxnp=0;
dfs(x,0);
d=0;
int g[4];
for(int y:G[maxnp])
if(!vis[y]){
g[++d]=y;
if(y==f[maxnp]){
siz[y]=n-siz[maxnp];
}
}
if(d==1){
cout<<"? "<<maxnp<<" "<<g[1]<<endl;
cin>>op;
vis[maxnp]=1;
if(!op){
return maxnp;
}else{
return work(g[1]);
}
}
if(d==2){
cout<<"? "<<g[1]<<" "<<g[2]<<endl;
cin>>op;
vis[maxnp]=1;
if(op==1){
return maxnp;
}else{
if(!op){
n=siz[g[1]];
return work(g[1]);
}else{
n=siz[g[2]];
return work(g[2]);
}
}
}
cout<<"? "<<g[1]<<" "<<g[2]<<endl;
cin>>op;
if(op==1){
vis[g[1]]=vis[g[2]]=1;
n=siz[g[3]]+1;
return work(maxnp);
}
vis[maxnp]=1;
if(op==0){
n=siz[g[1]];
return work(g[1]);
}
n=siz[g[2]];
return work(g[2]);
}
signed main(){
ios::sync_with_stdio(0);
cin>>T;
while(T--){
cin>>n;
for(int i=1;i<=n;++i)G[i].clear();
for(int i=1,x,y;i<=n;++i){
cin>>x>>y;
vis[i]=0;
if(x)
G[i].pb(x),G[x].pb(i);
if(y)
G[i].pb(y),G[y].pb(i);
}
cout<<"! "<<work(1)<<endl;
}
}
详细
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 9928kb
input:
2 5 0 0 1 5 2 4 0 0 0 0
output:
! ? 1 5
result:
wrong answer There are 5 candidates. (test case 1)