QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#733263#9570. Binary Treeqinglu09#RE 0ms0kbC++232.8kb2024-11-10 17:58:552024-11-10 17:58:56

Judging History

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

  • [2024-11-10 17:58:56]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-10 17:58:55]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>pii;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
// #define endl '\n'
#define debug(x) cout<<#x<<": "<<x<<endl
const int N=2e5+10;
typedef pair<ll,ll>PLL;

int num=0;
ll n;
int ask(int x, int y)
{
    if(++num>__lg(n)) exit(0);
    cout << "? " << x << ' ' << y << endl;
    cout.flush();
    cin >> x;
    return x;
}

void answer(int x)
{
    cout << "! " << x << endl;
    cout.flush();
}

vector<int>e[N];
int siz[N],del[N];
int sum,root,mxs;

int getsum(int u,int fa)
{
    siz[u]=1;
    for(auto v:e[u])
    {
        if(v==fa||del[v]) continue;
        getsum(v,u);
        siz[u]+=siz[v];
    }
    return siz[u];
}

void getroot(int u,int fa)
{
    siz[u]=1;
    int s=0;
    for(auto v:e[u])
    {
        if(v==fa||del[v]) continue;
        getroot(v,u);
        siz[u]+=siz[v];
        s=max(s,siz[v]);
    }
    s=max(s,sum-siz[u]);
    if(s<mxs) mxs=s,root=u;
}

void init(int u)
{
    sum=getsum(u,0);
    mxs=0;
}

void solve()
{
    num=0;
    cin >> n;
    for(int i=1;i<=n;i++) e[i].clear(),del[i]=0,siz[i]=0;
    sum=root=mxs=0;
    rep(i, 1, n)
    {
        int u, v;
        cin >> u >> v;
        if(u!=0)
        {
            e[i].push_back(u);
            e[u].push_back(i);
        }
        if(v!=0)
        {
            e[i].push_back(v);
            e[v].push_back(i);
        }
    }
    root=1;
    while(1)
    {
        init(root);
        getroot(root,0);
        cout.flush();
        int x=-1,y=-1;
        for(auto p:e[root])
        {
            if(!del[p])
            {
                if(x==-1) x=p;
                else if(y==-1) y=p;
            }
        }
        if(x==-1)
        {
            answer(root);
            return;
        }
        if(y==-1)
        {
            int ans=ask(x,root);
            if(ans==0)
            {
                answer(x);
                return;
            }
            if(ans == 2)
            {
                answer(root);
                return;
            }
        }
        int ans=ask(x,y);
        if(ans==0)
        {
            del[root]=1;
            del[y]=1;
            root=x;
        }
        if(ans==1)
        {
            del[x]=1;
            del[y]=1;
            root=root;
        }
        if(ans==2)
        {
            del[x]=1;
            del[root]=1;
            root=y;
        }
    }
}


int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
// #ifndef  ONLINEJUDGE
//     freopen("test.in","r",stdin);
//     freopen("test.out","w",stdout);
// #endif
    int T;
    cin>>T;
    while(T--)
    {
        solve();
    }

    return 0;
}

详细

Test #1:

score: 0
Runtime Error

input:

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

output:

? 2 1
! 2

result: