QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#770105#9570. Binary TreeeastcloudTL 0ms3464kbC++202.5kb2024-11-21 20:41:532024-11-21 20:41:53

Judging History

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

  • [2024-11-21 20:41:53]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3464kb
  • [2024-11-21 20:41:53]
  • 提交

answer


#include<bits/stdc++.h>

#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define cpy(x,y,s) memcpy(x,y,sizeof(x[0])*(s))
#define mset(x,v,s) memset(x,v,sizeof(x[0])*(s))
#define all(x) begin(x),end(x)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ary array
#define eb emplace_back
#define IL inline

using namespace std;

#define N 300005

int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0' || ch>'9')f=(ch=='-'?-1:f),ch=getchar();
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return x*f;
}
void write(int x){
    if(x<0)x=-x,putchar('-');
    if(x/10)write(x/10);
    putchar(x%10+'0');
}

vi e[N];
int vis[N],siz[N],tot;
int rt,maxn;

void find_root(int x,int fa){
    int maxsiz=0;siz[x]=1;
    for(auto v:e[x]){
        if(v==fa || vis[v])continue;
        find_root(v,x);siz[x]+=siz[v];
        maxsiz=max(maxsiz,siz[v]);
    }
    maxsiz=max(maxsiz,tot-siz[x]);
    if(maxsiz<maxn)maxn=maxsiz,rt=x;
}

void solve(){
    int n=read();
    for(int i=1;i<=n;i++)e[i].clear(),vis[i]=0;
    for(int i=1;i<=n;i++){
        int ls=read(),rs=read();
        if(ls)e[i].pb(ls),e[ls].pb(i);
        if(rs)e[i].pb(rs),e[rs].pb(i);
    }
    int x=1,cnt=0;
    while(1){
        vi S;cnt++;
        assert(cnt<=__lg(n)+3);
        for(auto v:e[x])if(!vis[v])S.pb(v);
        if(!S.size()){cout<<"! "<<x<<endl;break;}
        find_root(x,0);rt=0;maxn=n+5;tot=siz[x];find_root(x,0);
        x=rt;S.clear();
        //cerr<<rt<<' '<<S.size()<<' '<<tot<<"**"<<endl;
        for(auto v:e[x])if(!vis[v])S.pb(v);
        if(S.size()==1){
            int v=e[x][0];
            cout<<"? "<<x<<' '<<v<<endl;int res;cin>>res;
            if(res==0)cout<<"! "<<x<<endl;
            else cout<<"! "<<v<<endl;break;
        }
        else if(S.size()==2){
            int u=S[0],v=S[1];
            cout<<"? "<<u<<' '<<v<<endl;int res;cin>>res;
            if(res==1){cout<<"! "<<x<<endl;break;}             
            else if(res==0)vis[x]=1,x=u;
            else vis[x]=1,x=v;
        }
        else{
            sort(all(S),[](int x,int y){return siz[x]>siz[y];});
            cout<<"? "<<S[0]<<' '<<S[1]<<endl;int res;cin>>res;
            if(res==1)vis[S[0]]=vis[S[1]]=1;
            else if(res==0)vis[x]=1,x=S[0];
            else vis[x]=1,x=S[1];
        }
    }
}

int main(){

    int T=read();while(T--)solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3464kb

input:

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

output:

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

output:

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

result: