QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#751910 | #9570. Binary Tree | dog207 | WA | 6ms | 11284kb | C++14 | 1.6kb | 2024-11-15 21:15:19 | 2024-11-15 21:15:21 |
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);
}
int x=work(1);
cout<<"! "<<x<<endl;
}
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 9920kb
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
Wrong Answer
time: 6ms
memory: 11284kb
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 1 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 5 ? 2 7 ? 1 2 ! 2 ? 5 3 ? 1 4 ? 3 2 ! 3 ? 1 6 ? 1 7 ? 5 1 ! 1 ? 4 5 ? 3 1 ! 1 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 4 5 ! 5 ? 1 2 ? 3 5 ! 3 ? 3 2 ! 2 ? 2 1 ! 2 ? 2 3 ! 3 ? 2 6 ? 1 9 ? 10 9 ! 9 ? 2 1 ! 2 ? 5 9 ? 4 8 ? 5 3 ! 5 ? 5 8 ? 7 1 ? 5 3 ! 5 ? 3 4 ? 1 7 ? 8 2 ! 2 ? 2 1 ! 1 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 2 3 ? 4...
result:
wrong answer Too many queries (test case 90)