QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#736007#9570. Binary Treexbzxzn#TL 0ms0kbC++173.3kb2024-11-11 23:29:272024-11-11 23:29:27

Judging History

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

  • [2024-11-11 23:29:27]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-11 23:29:27]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long int
#define ull unsigned long long
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int maxn=1e5+5;
const int inf=1e18;
int n;
vector<int>edge[maxn];
int ask(int u,int v)
{
    cout<<"? "<<u<<" "<<v<<endl;
    int vv;cin>>vv;
    return vv;
}
int dep[maxn];
bool vis[maxn];
void dfs(int x,int pre)
{
    dep[x]=dep[pre]+1;
    for(auto j:edge[x])
    {
        if(j==pre||vis[j])continue;
        dfs(j,x);
    }
}
vector<int>g;int aim;
bool dfs2(int x,int pre)
{
    if(x==aim)
        return true;
    for(auto j:edge[x])
    {
        if(j==pre||vis[j])continue;
        if(dfs2(j,x))
        {
            g.pb(j);
            return true;
        }
    }
    return false;
}
void dfs3(int x,int pre)
{
    vis[x]=true;
    for(auto j:edge[x])
    {
        if(j==pre||vis[j])continue;
        dfs3(j,x);
    }
}
void solve()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        edge[i].clear();vis[i]=false;
    }
    for(int i=1;i<=n;i++)
    {
        int x,y;cin>>x>>y;
        if(x)
        {
            edge[i].pb(x);
            edge[x].pb(i);
        }
        if(y)
        {
            edge[i].pb(y);
            edge[y].pb(i);
        }
    }
    bool flag=true;
    while(flag)
    {
        g.clear();
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                dfs(i,0);break;
            }
        }
        int maxp=0;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                if(dep[i]>dep[maxp])
                {
                    maxp=i;
                }
            }
        }
        dfs(maxp,0);
        int qi=maxp;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                if(dep[i]>dep[maxp])
                {
                    maxp=i;
                }
            }
        }
        aim=maxp;
        g.pb(qi);
        dfs2(qi,0);
        if(g.size()%2==0)
        {
            int mid=(int)g.size()/2;
            int op=ask(g[mid-1],g[mid]);
            if(op==0)
            {
                dfs3(g[mid],g[mid-1]);
            }
            else
            {
                dfs3(g[mid-1],g[mid]);
            }

        }
        else
        {
            if(g.size()==1)
            {
                cout<<"! "<<g[0]<<endl;return ;
            }
            else
            {
                int mid=(int)g.size()/2;
                int op=ask(g[mid-1],g[mid+1]);
                if(op==1)
                {
                    dfs3(g[mid-1],g[mid]);
                    dfs3(g[mid+1],g[mid]);
                }
                else if(op==0)
                {
                    dfs3(g[mid+1],g[mid]);
                    dfs3(g[mid],g[mid-1]);
                }
                else if(op==2)
                {
                    dfs3(g[mid-1],g[mid]);
                    dfs3(g[mid],g[mid+1]);
                }
            }
        }
    }
}
signed main()
{
//    ios::sync_with_stdio(false);
//    cin.tie(nullptr);
//    cout.tie(nullptr);
    cout<<fixed<<setprecision(10);
    int T=1;
    cin>>T;
    for(int i=1;i<=T;i++)
        solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

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

output:

? 1 2
? 5 2
? 4 3

result: