QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#765055#9570. Binary Treejdyt11RE 0ms0kbC++232.5kb2024-11-20 11:49:352024-11-20 11:49:35

Judging History

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

  • [2024-11-20 11:49:35]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-20 11:49:35]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
#define inf 0x3f3f3f3f
#define ull unsigned long long
#define pll pair<ll,ll>
#define ls d*2
#define rs d*2+1
#define mid (l+r)/2
#define lowbit(x) (x&(-x))
//#define endl "\n"
#define all(x) x.begin(),x.end()
//#define int long long
//mt19937 seed;
//uniform_int_distribution<int>num(0,2e9);
const int N=1e6+10;
const int M=33;


int q(int u,int v){
    cout<<"? "<<u<<" "<<v<<endl;
    int x;
    cin>>x;
    return x;
}
int top=1;
vector<int>e[N];
int sz[N],f[N],dep[N];
void dfs(int u,int fa){
    dep[u]=dep[fa]+1;
    sz[u]=1;
    f[top]=dep[u]-dep[top];
    for(int v:e[u])if(v!=fa)dfs(v,u),sz[u]+=sz[v];
}
void dfs2(int u,int fa){
    for(int v:e[u])if(v!=fa)f[v]=f[u]+sz[top]-2*sz[v],dfs2(v,u);
}
bool cmp(int u,int v){
    return sz[u]>sz[v];
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int _=1;cin>>_;
    while(_--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)e[i].clear(),f[i]=sz[i]=dep[i]=0;
        for(int i=1;i<=n;i++){
            int l,r;
            cin>>l>>r;
            if(l)e[i].push_back(l),e[l].push_back(i);
            if(r)e[i].push_back(r),e[r].push_back(i);
        }
        int tot=n;
        top=1;
        while(tot>1){
            if(tot==2){
                int u=top,v=e[top][0];
                int res=q(u,v);
                if(res==0)top=u;
                else top=v;
                break;
            }
            dfs(top,0);
            dfs2(top,0);
            int tt=1;
            for(int i=2;i<=n;i++)if(f[i]<f[tt])tt=i;
            dfs(tt,0);
            sort(all(e[tt]),cmp);
            int u=e[tt][0],v=e[tt][1];
            int res=q(u,v);
            if(res==0){
                vector<int>temp;
                for(int a:e[u])if(a!=tt)temp.push_back(a);
                swap(temp,e[u]);
                top=u;
                tot=sz[u];
            }
            else if(res==1){
                vector<int>temp;
                for(int a:e[tt])if(a!=u&&a!=v)temp.push_back(a);
                swap(temp,e[tt]);
                tot=sz[tt]-sz[u]-sz[v];
                top=tt;
            }
            else{
                vector<int>temp;
                for(int a:e[v])if(a!=tt)temp.push_back(a);
                swap(temp,e[v]);
                tot=sz[v];
                top=v;
            }
            for(int i=1;i<=n;i++)sz[i]=f[i]=dep[i]=0;
        }
        cout<<top<<endl;
    }
}

详细

Test #1:

score: 0
Runtime Error

input:

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

output:

? 3 1
? 2 5
2

result: