QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#751464 | #9570. Binary Tree | Crying# | RE | 0ms | 0kb | C++14 | 1.6kb | 2024-11-15 18:50:43 | 2024-11-15 18:50:45 |
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
int T,n,vis[N];
vector<int> e[N];
void link(int x,int y){e[x].push_back(y); e[y].push_back(x);}
int dep[N],x,y,dx[N],dy[N];
void dfs(int u,int fa){
if(!vis[u])return;
for(auto v : e[u])if(v != fa){
dep[v] = dep[u] + 1;
dfs(v,u);
}
}
void solve(){
scanf("%d",&n); int cnt = 0;
for(int i=1;i<=n;i++)e[i].clear(),vis[i] = 1;
for(int i=1;i<=n;i++){
int x,y; scanf("%d%d",&x,&y);
if(x)link(i,x); if(y)link(i,y);
}
while(1){
int cnt = 0,ans = 0;
for(int i=1;i<=n;i++)if(vis[i])cnt++,ans = i;
if(cnt == 1){
printf("! %d\n",ans); fflush(stdout);
return;
}
x = y = 0;
dep[ans] = 0; dfs(ans,0);
for(int i=1;i<=n;i++)if(vis[i] && dep[i] > dep[x])x = i;
dep[x] = 0; dfs(x,0);
for(int i=1;i<=n;i++)if(vis[i] && dep[i] > dep[y])y = i;
for(int i=1;i<=n;i++)dx[i] = dep[i];
dep[y] = 0; dfs(y,0);
for(int i=1;i<=n;i++)dy[i] = dep[i];
//
cnt++;
if((1LL<<cnt) > n){
assert(0);
}
printf("? %d %d\n",x,y); fflush(stdout);
int res; scanf("%d",&res);
for(int i=1;i<=n;i++)if(vis[i]){
if(res == 0)vis[i] = (dx[i] < dy[i]);
else if(res == 1)vis[i] = (dx[i] == dy[i]);
else vis[i] = (dx[i] > dy[i]);
}
}
}
int main(){
scanf("%d",&T);
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 5 0 0 1 5 2 4 0 0 0 0