QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#757077#9570. Binary Treehlzy_aweiWA 5ms11836kbC++143.0kb2024-11-16 23:56:532024-11-16 23:56:53

Judging History

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

  • [2024-11-16 23:56:53]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:11836kb
  • [2024-11-16 23:56:53]
  • 提交

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;
void dfs1(int x,int fa,int step)
{
    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;
ll hh[200005];
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);
            }
            
        }
        dfs1(1,0,1);
        dfs(s,0);
        mx=0;
        int l=s,r;
        dfs1(s,0,1);
        r=s;
        int cc=0;
        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({lenr-lenl,i});
                    }
                }
                else if(ch==2)
                {
                    if(lenl>lenr&&hh[i]==cc)
                    {
                        ans.insert({lenl-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.end();
            it--;
            r=it->second;
            l=ans.begin()->second;
            cc++;
        }
        cout<<"! "<<l<<endl;
    }
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 11836kb

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 5ms
memory: 11828kb

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
2
0
2
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
2
1
0
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
2
0
0
5
4 5
3 1
0 0
0 0
0 0
2
0

output:

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

result:

wrong answer Too many queries (test case 4)