QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#730333#9570. Binary Treeucup-team4074#RE 0ms0kbC++202.6kb2024-11-09 19:48:522024-11-09 19:48:52

Judging History

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

  • [2024-11-09 19:48:52]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-09 19:48:52]
  • 提交

answer

#include<bits/stdc++.h>

//#define int long long

using namespace std;

void solve();

signed main() {
    int t = 1;
    cin >> t;
    while (t--) solve();
}

const int N = 2e5 + 10;
const int maxn=100010;
vector<int>e[maxn];
int v[maxn];
int rt=0,rtmx=1e9,s[maxn],sum;
int n;
int dfs(int x,int fa)
{
    int sum1=1;
    for(int i=0;i<e[x].size();i++)
    {
        if(!v[e[x][i]]||e[x][i]==fa)continue;
        sum1+=dfs(e[x][i],x);
    }
    return sum1;
}
void getroot(int x,int fa)
{
    if(fa==0)
    {
        rt=0;
        rtmx=1e9;
    }
    s[x]=1;
    int mx=0;
    for(int i=0;i<e[x].size();i++)
    {
        int y=e[x][i];
        if(y==fa||v[y]==0)continue;
        getroot(y,x);
        s[x]+=s[y];
        mx=max(mx,s[y]);
    }
    mx=max(mx,sum-s[x]);
    if(mx<rtmx)
    {
        rt=x;
        rtmx=mx;
    }
}
int ask(int x,int y)
{
    cout<<"? "<<x<<" "<<y<<"\n";
    cout.flush();
    int r;
    cin>>r;
    return r;
}
void solve() {
    cin>>n;
    sum=n;
    for(int i=1;i<=n;i++)
    {
        e[i].clear();
        v[i]=1;
        s[i]=0;

    }
    for(int i=1;i<=n;i++)
    {
        int u,v;
        cin>>u>>v;
        if(u)
        {
            e[i].push_back(u);
            e[u].push_back(i);
        }
        if(v)
        {
            e[v].push_back(i);
            e[i].push_back(v);
        }
    }
    getroot(1,0);
    int id=1;
    while((1<<id)<=n)
    {
        id++;
        int x=0,y=0;
        for(int i=0;i<e[rt].size();i++)
        {
            if(!v[e[rt][i]])continue;
            if(!x)x=e[rt][i];
            else if(!y)y=e[rt][i];
        }
        if(!x&&!y)
        {
            cout<<"! "<<rt<<"\n";
            cout.flush();
            return;
        }
        else if(!y)
        {
            if(ask(rt,x)==0)
            {
                cout<<"! "<<rt<<"\n";
                cout.flush();
                return;
            }
            else
            {
                cout<<"! "<<x<<"\n";
                cout.flush();
                return;
            }
        }
        int vi=ask(x,y);
        if(vi==0)
        {
            v[rt]=0;
            sum=dfs(x,0);
            getroot(x,0);
        }
        else if(vi==1)
        {
            v[x]=0;
            v[y]=0;
            sum=dfs(rt,0);
            getroot(rt,0);
        }
        else
        {
            v[rt]=0;
            sum=dfs(y,0);
            getroot(y,0);
        }
    }
    cout<<"! "<<0<<"\n";
    cout.flush();
}

詳細信息

Test #1:

score: 0
Runtime Error

input:

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

output:

? 1 5
? 2 4
! 0

result: