QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#757132#9570. Binary Treehlzy_aweiWA 2ms11848kbC++143.2kb2024-11-17 00:34:252024-11-17 00:34:27

Judging History

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

  • [2024-11-17 00:34:27]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:11848kb
  • [2024-11-17 00:34:25]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <unordered_map>
#include <cmath>
#include <queue>
#include <deque>
#include <set>
typedef long long int ll;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int>pii;

const ll N = 1e6+10, INF = 1e9,mod=998244353;
int h[N],ne[N],e[N],it,deth[N],dp[N][20];
int t;
int n,m,s;
void add(int a,int b)
{
	e[++it]=b;
	ne[it]=h[a];
	h[a]=it;
}
void dfs(int x,int fa)
{
	deth[x]=deth[fa]+1;
	dp[x][0]=fa;
	for(int i=1;i<20;i++)
	{
		dp[x][i]=dp[dp[x][i-1]][i-1];
	}
	for(int i=h[x];i;i=ne[i])
	{
		if(e[i]!=fa)dfs(e[i],x);
	}
}
int lca(int x,int y)
{
	if(deth[y]>deth[x])swap(y,x);
	for(int i=19;i>=0;i--)
	{
		if(deth[dp[x][i]]>=deth[y])x=dp[x][i];
	}
	if(x==y)return x;
	for(int i=19;i>=0;i--)
	{
		if(dp[x][i]!=dp[y][i])y=dp[y][i],x=dp[x][i];
	}
	return dp[x][0];
}
int mx;
int cc;
int hh[200005];
void dfs1(int x,int fa,int step)
{
    if(hh[x]!=cc)
        return;
    // cout<<x<<" ";
    if(step>mx)
    {
        mx=step;
        s=x;
    }
    for(int i=h[x];i;i=ne[i])
	{
		if(e[i]!=fa)
            dfs1(e[i],x,step+1);
	}
}
int ch;
set<pair<ll,ll>> ans;

int main()
{
    cin>>t;
    while(t--)
    {
        mx=0;
        it=0;
        cin>>n;
        for(int i=1;i<=n;i++)
            h[i]=0,hh[i]=0,deth[i]=0;
        for(int i=1;i<=n;i++)
        {
            int u,v;
            cin>>u>>v;
            if(u)
            {
                add(u,i);
                add(i,u);
            }
            if(v)
            {
                add(i,v);
                add(v,i);
            }
            
        }
        cc=0;
        dfs1(1,0,1);
        dfs(s,0);
        mx=0;
        int l=s,r;
        dfs1(s,0,1);
        r=s;
        while(l!=r)
        {
            cout<<"? "<<l<<" "<<r<<endl;
            cin>>ch;
            ans.clear();
            for(int i=1;i<=n;i++)
            {
                int fl=lca(i,l),fr=lca(i,r);
                int lenl=deth[i]+deth[l]-2*deth[fl];
                int lenr=deth[i]+deth[r]-2*deth[fr];
                // cout<<lenl<<" "<<lenr<<"\n";
                if(ch==0)
                {
                    if(lenl<lenr&&hh[i]==cc)
                    {
                        ans.insert({lenl,i});
                    }
                }
                else if(ch==2)
                {
                    if(lenl>lenr&&hh[i]==cc)
                    {
                        ans.insert({lenr,i});
                    }
                }
                else
                {
                    if(lenl==lenr&&hh[i]==cc)
                        ans.insert({lenl-lenr,i});
                }
            }
            // cout<<ans.size();
            for(auto i:ans)
                hh[i.second]++;
            auto it=ans.begin();
            cc++;
            mx=0;
            // cout<<"\n";
            dfs1(it->second,0,1);
            // cout<<"\n";
            l=s;
            mx=0;
            dfs1(l,0,1);
            // cout<<"\n";
            r=s;
            cout<<l<<" "<<r<<"\n";
        }
        cout<<"! "<<l<<endl;
    }
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 11848kb

input:

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

output:

? 4 5
1 5
? 1 5

result:

wrong answer Token parameter [name=op] equals to "1", doesn't correspond to pattern "[?!]{1,1}" (test case 1)