QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#760982#9570. Binary TreeStelorTL 1ms3608kbC++203.0kb2024-11-18 20:29:352024-11-18 20:29:36

Judging History

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

  • [2024-11-18 20:29:36]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3608kb
  • [2024-11-18 20:29:35]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1e5+10;
vector<int> v[N];
int n,num,maxnum,root,d[N],ans[N];
bool vis[N];
void dfs(int u,int fa){
    d[u]=1;
    int tmp=0;
    for(auto i:v[u]){
        if(i==fa||vis[i]) continue;
        dfs(i,u);
        d[u]+=d[i];
        tmp=max(tmp,d[i]);
    }
    tmp=max(tmp,n-d[u]);
    if(tmp<maxnum){
        maxnum=tmp;
        num=0;
        ans[++num]=u;
    }else if(tmp==maxnum) ans[++num]=u;

}
void answer(int x){
    cout<<"! "<<x<<endl;
}
int query(int x,int y){
    cout<<"? "<<x<<" "<<y<<endl;
    int ret;
    cin >> ret;
    return ret;
}
int get_num(int u,int fa){
    int ret=1;
    for(auto j:v[u]){
        if(j==fa||vis[j])continue;
        ret+=get_num(j,u);
    }
    return ret;
}
bool work(){
    dfs(root,0);
    int tmp=0;
    vector<int> nxt;
  
    for(auto i:v[ans[1]]){
        if(!vis[i]) {
            tmp++;
            nxt.push_back(i);
        }
    }
    if(tmp==0){
        answer(ans[1]);
        return true;
    }else if(tmp==1){
        auto now=nxt[0];
        int op=query(ans[1],now);
        if(op==0){
            answer(ans[1]);
            return true;
        }
        if(op==2){
            answer(now);
            return true;
        }
    }else if(tmp==2){
        int op=query(nxt[0],nxt[1]);
        if(op==1){
            answer(ans[1]);
            return true;
        }else if(op==0){
            vis[ans[1]]=true;
            root=nxt[0];
        }else if(op==2){
            vis[ans[1]]=true;
            root=nxt[1];
        }
    }else if(tmp==3){
        vis[ans[1]]=true;
        int num1=get_num(nxt[0],0);
        int num2=get_num(nxt[1],0);
        int num3=get_num(nxt[2],0);

        vector<pair<int,int>> v;
        v.push_back({num1,nxt[0]});
        v.push_back({num2,nxt[1]});
        v.push_back({num3,nxt[2]});
        sort(v.begin(),v.end(),greater<pair<int,int>>());
        int op=query(v[0].second,v[1].second);
        if(op==0){
            root=v[0].second;
        }else if(op==2){
            root=v[1].second;
        }else if(op==1){
            vis[ans[1]]=false;
            vis[v[0].second]=true;
            vis[v[1].second]=true;
            root=ans[1];
            
        }
    }
    return false;
}

void solve(){
    cin >> n;
    for(int i=1;i<=n;++i) {
        vis[i]=false;
        v[i].clear();
    }
    num=0;
    maxnum=1e9;
    root=1;
    for(int i=1;i<=n;++i){
        int l,r;
        cin >> l >> r;
        if(l) v[i].push_back(l),v[l].push_back(i);
        if(r) v[i].push_back(r),v[r].push_back(i);
    }
    while(true){
        for(int i=1;i<=n;++i) d[i]=0;
        ans[1]=0;
        ans[2]=0;
        num=0;
        maxnum=1e9;
        if(work()) break;
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    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: 3608kb

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Time Limit Exceeded

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

output:

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

result: