QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#736134#9570. Binary TreewxgmjfhyRE 1ms3632kbC++202.8kb2024-11-12 01:11:382024-11-12 01:11:38

Judging History

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

  • [2024-11-12 01:11:38]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3632kb
  • [2024-11-12 01:11:38]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
 
int n,x,CNT;

inline int query(int a,int b){
    CNT++;

    cout<<"? "<<a<<" "<<b<<endl;
    cin>>x;
    return x;
}

inline void solve(){
    cin>>n;

    CNT=0;
    int lim=log2(n);

    vector<vector<int> >g(n+1);
    for(int i=1;i<=n;i++){
        int a,b;
        cin>>a>>b;
        if(a)g[a].push_back(i),g[i].push_back(a);
        if(b)g[b].push_back(i),g[i].push_back(b);
    }

    vector<int>mx(n+1),sz(n+1),f(n+1);
    mx[0]=inf;

    int sum=n,rt=0;

    auto find=[&](auto self,int u,int fa)->void {
        sz[u]=1,mx[u]=0;
        for(auto t:g[u]){
            if(t==fa||f[t])continue;
            self(self,t,u);
            sz[u]+=sz[t];
            mx[u]=max(mx[u],sz[t]);
        }
        mx[u]=max(mx[u],sum-sz[u]);
        if(mx[u]<mx[rt])rt=u;
    };
    find(find,1,-1);

    auto dfs=[&](auto self,int u,int fa)->void {
        sz[u]=1;
        for(auto t:g[u]){
            if(t==fa||f[t])continue;
            self(self,t,u);
            sz[u]+=sz[t];
        }
    };

    int ans=-1;

    auto wk=[&](auto self,int u)->void {
        dfs(dfs,u,-1);
        
        int cnt=0;
        for(auto t:g[u]){
            if(f[t])continue;
            cnt++;
        }

        if(cnt==0){
            ans=u;
            return;
        }

        if(cnt==1){
            for(auto t:g[u]){
                if(f[t])continue;

                if(CNT==lim){
                    ans=1e9;
                    return;
                }

                ans=(query(t,u)==0?t:u);
                return;
            }
        }

        vector<int>son;
        for(auto t:g[u]){
            if(f[t])continue;
            son.push_back(t);
        }

        sort(son.begin(),son.end(),[&](int x,int y){
            return sz[x]>sz[y];
        });

        int son1=son[0],son2=son[1];

        if(CNT==lim){
            ans=1e9;
            return;
        }

        int x=query(son1,son2);

        if(x==0){
            f[u]=1;
            sum=sz[son1],rt=0;
            find(find,son1,-1);
            self(self,son1);
        }else if(x==2){
            f[u]=1;
            sum=sz[son2],rt=0;
            find(find,son2,-1);
            self(self,son2);
        }else{
            if(son.size()==2){
                ans=u;
                return;
            }

            f[son1]=f[son2]=1;
            sum=sz[son[2]]+1,rt=0;
            find(find,u,-1);
            self(self,u);
        }
    };
    wk(wk,rt);

    cout<<"! "<<ans<<endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    // cout<<setiosflags(ios::fixed)<<setprecision(10);

    int t=1;
    cin>>t;
    while(t--)solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

? 3 1
? 5 2
! 5
? 1 2
! 2

result:

ok OK (2 test cases)

Test #2:

score: -100
Runtime Error

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
0
0

output:

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

result: