QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#738160#9570. Binary TreeAlbert711TL 0ms0kbC++202.9kb2024-11-12 17:59:292024-11-12 17:59:29

Judging History

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

  • [2024-11-12 17:59:29]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-12 17:59:29]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
//#define int long long

vector<vector<int> > ed;

int q(int u, int v) {
    cout << "? " << u << ' ' << v << endl;
    int tmp;
    cin >> tmp;
    return tmp;
}

vector<int> fa;
vector<int> dep;
vector<int> idx;
int t1,t2;
void dfs1(int u, int f) {
    dep[u]=dep[f]+1;
    fa[u]=f;
    if(dep[u]>dep[t1]) t1=u;
    for(auto i:ed[u]){
        if(i==f) continue;
        if(!idx[i]) dfs1(i,u);
    }
}
void dfs2(int u, int f) {
    dep[u]=dep[f]+1;
    fa[u]=f;
    if(dep[u]>dep[t2]) t2=u;
    for(auto i:ed[u]){
        if(i==f) continue;
        if(!idx[i]) dfs2(i,u);
    }
}
void solve() {
    int n;
    cin >> n;
    ed = vector<vector<int> >(n + 1);
    fa = vector<int>(n + 1);
    dep = vector<int>(n + 1);
    idx = vector<int>(n + 1);
    for (int i = 1; i <= n; i++) {
        int a, b;
        cin >> a >> b;
        if (a != 0) {
            ed[i].push_back(a);
            ed[a].push_back(i);
        }
        if (b != 0) {
            ed[i].push_back(b);
            ed[b].push_back(i);
        }
    }
    int ys=1;
    while(1){
        t1=0;t2=0;
        dfs1(ys,0);
        dfs2(t1,0);
//        cout<<t1<<' '<<t2<<'\n';
        if(t1==t2){
            cout<<"! "<<t1<<endl;
            return;
        }
        int tp1=t1,tp2=t2;
        while(t1!=t2){
            if(dep[t1]>dep[t2]) swap(t1,t2);
            int len=dep[t2]-dep[t1];
            if(len%2==0){
                len/=2;
                int tmp=q(t1,t2);
                if(tmp==0){
                    for(int i=1;i<=len+1;i++){
                        t2=fa[t2];
                    }
                }else if(tmp==2){
                    t1=t2;
                    for(int i=1;i<=len-1;i++){
                        t2=fa[t2];
                    }
                }else{
                    for(int i=1;i<=len;i++){
                        t2=fa[t2];
                    }
                    t1=t2;
                    break;
                }
            }else {
                len/=2;
                int tmp = q(t1, t2);
                if (tmp == 0) {
                    for (int i = 1; i <= len + 1; i++) {
                        t2 = fa[t2];
                    }
                } else if (tmp == 2) {
                    t1=t2;
                    for (int i = 1; i <= len; i++) {
                        t2 = fa[t2];
                    }
                }
                if(dep[t1]>dep[t2]) swap(t1,t2);
            }
        }
        ys=t2;
        if(dep[tp1]>dep[tp2]) swap(tp1,tp2);
        while(dep[tp2]>=dep[tp1]){
            if(tp2!=ys) idx[tp2]=1;
            tp2=fa[tp2];
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int T = 1;
    cin >> T;
    while (T--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

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

output:

? 4 1
? 2 1
? 5 2

result: