QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#801587#9570. Binary TreeqlwpcTL 3ms14112kbC++143.0kb2024-12-07 03:07:402024-12-07 03:07:40

Judging History

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

  • [2024-12-07 03:07:40]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:14112kb
  • [2024-12-07 03:07:40]
  • 提交

answer

#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cassert>
#include<cmath>
using ll = long long;
using namespace std;
const int N = 400010;
const int M = 801040;
const int K = 510;
const int lgN = 21;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
int read(){
    int x=0,flag=1;char c;
    while((c=getchar())<'0' || c>'9') if(c=='-') flag=-1;
    while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return x*flag;
}
struct node{
    int to,next;
}q[M];
int head[N],ss,du[N],fa[N];int ls[N],rs[N];
int sz[N],mxs[N],Rt, curN;bool vis[N];
void addedge(int x,int y){
    ++du[x];++du[y];
    q[++ss]=(node){y,head[x]};head[x]=ss;
    q[++ss]=(node){x,head[y]};head[y]=ss;
}
int query(int x,int y)
{
    printf("? %d %d\n",x,y);
    fflush(stdout);
    return read();
}
void dfs1(int x,int fa)
{//printf("x=%d\n",x);
    sz[x]=mxs[x]=1;
    for (int j=head[x];j;j=q[j].next)
    {
        int t=q[j].to;
        if (t==fa||vis[t]) continue;
        dfs1(t,x);
        sz[x]+=sz[t];
        mxs[x]=max(mxs[x], sz[t]);
    }
    mxs[x]=max(mxs[x], curN-sz[x]);
    if (mxs[x]<mxs[Rt]) Rt=x;
}
int ans;
void solve(int x)
{
    if (curN==1) {ans=x;return;}
    dfs1(x, 0);
    vis[x]=true;
    int nxt;
    if (du[x]==3)
    {
        int dir=query(ls[x],rs[x]);
        if (dir==1){
            vis[ls[x]]=vis[rs[x]]=true;
            vis[x]=false;
            nxt=fa[x];
            curN=sz[fa[x]]+1;
        }else
        {
            if (dir==0) nxt=ls[x];
            else nxt=rs[x];
            curN=sz[nxt];
        }
    }else// du==2
    {
        int u=0,v=0;
        for (int j=head[x];j;j=q[j].next)
            if (u==0) u=q[j].to;
            else v=q[j].to;
        if (v==0) v=x;
        int dir=query(u,v);
        if (dir==1){
            ans=x;
            return;
        }else
        {
            if (dir==0) nxt=u;
            else nxt=v;
            if (du[x]<2)
            {
                if (nxt==x) {ans=x;return;}
                vis[u]=vis[v]=true;
                vis[nxt]=false;
            }
            curN=sz[nxt];
        }
    }
    Rt=0;
    dfs1(nxt,0);
    solve(Rt);
}
int main()
{
    int T=read();
    while(T--)
    {
        int n=read();
        for (int i=1;i<=n;++i)
        {
            int x=read(),y=read();
            ls[i]=x;rs[i]=y;
            if (x) addedge(i,x),fa[x]=i;
            if (y) addedge(i,y),fa[y]=i;
        }
        if (n==2)
        {
            int x=query(1,2);
            printf("! %d\n",x==0?1:2);
            fflush(stdout);
        }else
        {
            curN=n;mxs[0]=INF;Rt=0;
            dfs1(1,0);
        //printf("crush");
            solve(Rt);
            printf("! %d\n",ans);
            fflush(stdout);
        }
        ss=0;
        for (int i=1;i<=n;++i) head[i]=du[i]=vis[i]=0;
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 14112kb

input:

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

output:

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

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
1
2
0
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
0
1
1

output:

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

result: