QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#722173#9570. Binary TreeBeater#WA 0ms3632kbC++201.8kb2024-11-07 18:04:032024-11-07 18:04:06

Judging History

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

  • [2024-11-07 18:04:06]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3632kb
  • [2024-11-07 18:04:03]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'


int ask(int u,int v)
{
    
    cout<<"? "<<u<<" "<<v<<endl;
    int t;
    cin>>t;
    return t;
}
int n,st,ed;
int L[100010],R[100010],in[100010];
void dfs(int u)
{
    if(L[u]&&R[u])
    {
        int t=ask(L[u],R[u]);
        if(t==1) 
        {
            ed=u;
            return ;
        }
        else if(t==0) dfs(L[u]);
        else dfs(R[u]);
    }
    else if(L[u]||R[u])
    {
        if(L[u]) dfs(L[u]);
        else dfs(R[u]);
    }
    else 
    {
        ed=u;
        return ;
    }
}
int fa[100010];
void solve()
{    
    cin>>n;
    for(int i=1;i<=n;i++) in[i]=fa[i]=0;
    for(int i=1;i<=n;i++)
    {
        cin>>L[i]>>R[i];
        fa[L[i]]=i;
        fa[R[i]]=i;
        in[L[i]]++;
        in[R[i]]++;
    }
    int root;
    for(int i=1;i<=n;i++) 
    if(!in[i])
    {
        root=i;
        break;
    }
    
    ed=0;
    dfs(root);
    
    vector<int>path;
    path.push_back(ed);
    while(!L[fa[ed]]||!R[fa[ed]])
    {
        ed=fa[ed];
        if(ed==0) break;
        path.push_back(ed);
    }
    
    if(path.size()==1) cout<<"! "<<path[0]<<"\n";
    else
    {
        int l=0,r=path.size()-1;
        while(l<r)
        {
            int t=ask(path[l],path[r]);
            int mid=(l+r)/2;
            if(t==1)
            {
                cout<<"! "<<path[mid]<<"\n";
                return ;
            }
            else if(t==0) r=mid;
            else l=mid+1;
        }
        cout<<"! "<<path[l]<<"\n";
    }
}

int main (){
    ios::sync_with_stdio(0);
    // cin.tie(0);
    // cout.tie(0);
    cout.flush();
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
    
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3632kb

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
? 2 1
! 1

result:

ok OK (2 test cases)

Test #2:

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

input:

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

output:

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

result:

wrong answer Too many queries (test case 3)