QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#758119 | #9570. Binary Tree | wyhao | WA | 20ms | 3884kb | C++14 | 1.7kb | 2024-11-17 15:56:11 | 2024-11-17 15:56:11 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int n;
int ch[N][2],fa[N];
int rt,u,v;
int dis1[N],dis2[N];
bool vis[N];
vector<int> vec;
void dfs(int x,int d,int *dis){
dis[x]=d;
if(dis[x]>dis[u]) u=x;
if(!vis[fa[x]] and !dis[fa[x]]) dfs(fa[x],d+1,dis);
if(!vis[ch[x][0]] and !dis[ch[x][0]]) dfs(ch[x][0],d+1,dis);
if(!vis[ch[x][1]] and !dis[ch[x][1]]) dfs(ch[x][1],d+1,dis);
}
void dfs1(int x,int d,int *dis){
dis[x]=d;
vec.push_back(x);
if(!vis[fa[x]] and !dis[fa[x]]) dfs1(fa[x],d+1,dis);
if(!vis[ch[x][0]] and !dis[ch[x][0]]) dfs1(ch[x][0],d+1,dis);
if(!vis[ch[x][1]] and !dis[ch[x][1]]) dfs1(ch[x][1],d+1,dis);
}
bool dir(){
for(auto p:vec){
dis1[p]=0;
}
u=rt;
dfs(rt,1,dis1);
// printf("-%d\n",u);
for(auto p:vec){
dis1[p]=0;
dis2[p]=0;
}
v=u;
dfs(v,1,dis1);
// printf("--%d %d\n",u,v);
if(u==v) return false;
vec.clear();
dfs1(u,1,dis2);
return true;
}
void solve(){
scanf("%d",&n);
for(int i=1;i<=n;i++) fa[i]=0;
for(int i=1;i<=n;i++){
scanf("%d%d",&ch[i][0],&ch[i][1]);
fa[ch[i][0]]=i;
fa[ch[i][1]]=i;
vis[i]=false;
vec.push_back(i);
}
vis[0]=true;
rt = 1;
int s;
while(dir()){
printf("? %d %d\n",u,v);
fflush(stdout);
scanf("%d",&s);
if(s==1){
for(auto p:vec){
if(dis1[p] != dis2[p]) vis[p]=true;
else rt = p;
}
}else if(s==0){
for(auto p:vec){
if(dis1[p] <= dis2[p]) vis[p]=true;
else rt = p;
}
}else{
for(auto p:vec){
if(dis1[p] >= dis2[p]) vis[p]=true;
else rt = p;
}
}
}
printf("! %d\n",u);
fflush(stdout);
}
int main(){
int tests;
scanf("%d",&tests);
while(tests--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3848kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 0 1 2 0 2 0 0 2
output:
? 1 4 ? 5 1 ! 2 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 20ms
memory: 3884kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 1 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 2 1 0 5 4 5 3 1 0 0 0 0 0 0 0 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 1 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 1 3 3 0 1 0 0 0 2 2 2 0 0 0 2 3 2 3 0 0 0 0 1 10 2 8 9 7 0 0 ...
output:
? 7 3 ? 3 5 ? 4 3 ! 3 ? 1 6 ? 1 3 ? 4 2 ! 4 ? 7 4 ? 4 8 ? 2 6 ! 2 ? 4 3 ? 5 4 ! 5 ? 1 8 ? 4 1 ! 5 ? 3 4 ? 1 3 ! 3 ? 1 3 ? 2 1 ! 5 ? 3 2 ! 2 ? 1 2 ! 2 ? 3 2 ! 1 ? 8 4 ? 10 8 ? 9 10 ! 10 ? 1 2 ! 1 ? 6 4 ? 1 6 ? 6 2 ! 2 ? 9 6 ? 6 10 ? 2 6 ! 6 ? 1 6 ? 4 6 ? 5 6 ! 3 ? 1 2 ! 1 ? 7 2 ? 1 7 ! 1 ? 1 3 ? 7 1 ...
result:
wrong answer Too many queries (test case 20)