QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#795592#9570. Binary Treezhouyf828RE 2ms11832kbC++172.7kb2024-11-30 21:55:352024-11-30 21:55:36

Judging History

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

  • [2024-11-30 21:55:36]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:11832kb
  • [2024-11-30 21:55:35]
  • 提交

answer

#include<iostream>
#include<map>
#include<vector>
using namespace std;
int input;
const int N=1e6+10;
typedef pair<int,int> pii;
int depth[N];
int dep[N];
map<pii,int> mp;
int q[N],e[N*4],ne[N*4],idx;
void add(int a,int b){
    e[idx]=b;ne[idx]=q[a];q[a]=idx++;
}
void dfs1(int u,int fa){
    for(int i=q[u];i!=-1;i=ne[i]){
        int j=e[i];
        if(mp[{u,j}]) continue;
        if(j!=fa){
            depth[j]=depth[u]+1;
            dfs1(j,u);
        }
    }
}
void dfs2(int u,int fa){
    for(int i=q[u];i!=-1;i=ne[i]){
        int j=e[i];
        if(mp[{u,j}]) continue;
        if(j!=fa){
            dfs2(j,u);
            dep[u]=max(dep[u],dep[j]+1);
        }
    }
}
void solve(int po){
    int n;
    cin>>n;
    for(int i=0;i<=n+5;i++) q[i]=-1;
    idx=0;
    mp.clear();
    for(int i=1;i<=n;i++){
        int a,b;cin>>a>>b;
        if(a)add(i,a),add(a,i);
        if(b)add(i,b),add(b,i);
    }
    int rt=1;
    vector<int> ve;
    int ui=0;
    int jk=n;
    while(jk){
        ui++;
        jk>>=1;
    }
    while(ui--){
        ve.clear();
        for(int i=1;i<=n;i++) depth[i]=0;
        depth[rt]=1;
        dfs1(rt,0);
        for(int i=1;i<=n;i++){
            if(depth[rt]<depth[i]) rt=i;
        }
        for(int i=1;i<=n;i++) dep[i]=0;;
        dfs2(rt,0);
        int sum=dep[rt];
        if(sum==0){
            cout<<"! "<<rt<<endl;
            return;
        }
        int k=sum+1;int dq=rt;
        ve.clear();
        while(k--){
            ve.push_back(dq);
            for(int i=q[dq];i!=-1;i=ne[i]){
                int j=e[i];
                if(mp[{dq,j}]) continue;
                if(dep[j]==dep[dq]-1){
                    dq=j;
                    break;
                }
            }
        }
        int mid=sum/2;
        int ll=mid+1,rr=mid-1;
        if(ll>sum) ll=sum;
        if(rr<0) rr++;
        ll=ve[ll],rr=ve[rr],mid=ve[mid];
        cout<<"? "<<ll<<' '<<rr<<endl;
        cin>>input;
        if(input==0) {
            rt=ll;
            mp[{ll,mid}]=1;
            mp[{mid,ll}]=1;
            mp[{ll,rr}]=1;
            mp[{rr,ll}]=1;
        }
        else if(input==2) {
            rt=rr;
            mp[{rr,mid}]=1;
            mp[{mid,rr}]=1;
            mp[{ll,rr}]=1;
            mp[{rr,ll}]=1;
        }
        else {
            rt=mid;
            mp[{ll,mid}]=1;
            mp[{mid,ll}]=1;
            mp[{rr,mid}]=1;
            mp[{mid,rr}]=1;
        }
    }
    cout<<"! 0"<<endl;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t;cin>>t;
    for(int i=1;i<=t;i++){
        solve(i);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11832kb

input:

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

output:

? 2 4
? 5 1
! 2
? 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
1
0
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
0
0
2
5
4 5
3 1
0 0
0 0
0 0
0
2
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
1
1
0
5
3 0
5 1
0 0
0 0
4 0
0
0
5
5 0
0 0
0 0
3 0
2 4
0
2
3
3 0
1 0
0 0
0
2
2 0
0 0
2
3
2 3
0 0
0 0
1
10
2 8
9 7
0 ...

output:

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

result: