QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#725016#9570. Binary TreeTime_stopWA 1ms3652kbC++141.5kb2024-11-08 15:54:212024-11-08 15:54:23

Judging History

你现在查看的是最新测评结果

  • [2024-11-08 15:54:23]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3652kb
  • [2024-11-08 15:54:21]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int M=2e5+5;
int n;
struct no{
    int l,r;
}tree[M];
int fa[M];
void solve(){
    cin>>n;
    for(int i=1;i<=n;i++){
        tree[i].l=tree[i].r=fa[i]=0;
    }
    for(int i=1;i<=n;i++){
        cin>>tree[i].l>>tree[i].r;
        fa[tree[i].l]=i; fa[tree[i].r]=i; 
    }
    int root;
    for(int i=1;i<=n;i++){
        if(!fa[i]) {
            root=i; break;
        }
    }
    while(1){
        if(tree[root].l==tree[root].r&&tree[root].l==0){
            cout<<"! "<<root<<endl;
             cout.flush();
             break;
        }
        int u,v,tmp;
        if(tree[root].l==0||tree[root].r==0){
            u=root; 
            if(tree[root].l!=0) v=tree[root].l;
            else v=tree[root].r;
            cout<<"? "<<u<<" "<<v<<endl;
            cin>>tmp;
            if(tmp==0){
                cout<<"! "<<u<<endl;
                 cout.flush();
                 break;
            }
            else if(tmp==2){
                root=v; continue;
            }
        } else {
            int u=tree[root].l,v=tree[root].r;
            cout<<"? "<<u<<" "<<v<<endl;
            cin>>tmp;
            if(tmp==1){
                cout<<"! "<<root<<endl;
                 cout.flush();
                 break;
            } else if(tmp==0){
                root=u; continue;
            } else if(tmp==2){
                root=v; continue;
            }
        }
    }
}
int main(){
    int t;cin>>t;
    while(t--){
        solve();
    }
    
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3620kb

input:

2
5
0 0
1 5
2 4
0 0
0 0
0
1
2
0 2
0 0
2

output:

? 2 4
? 1 5
! 2
? 1 2
! 2

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3652kb

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
2
0
2

output:

? 1 2
? 8 6
? 5 4
? 4 3

result:

wrong answer Too many queries (test case 1)