QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#785709#9570. Binary TreeGodwangTL 0ms0kbC++237.8kb2024-11-26 18:53:472024-11-26 18:53:48

Judging History

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

  • [2024-11-26 18:53:48]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-26 18:53:47]
  • 提交

answer

#include <iostream>
using namespace std;
#include <set>
#include <algorithm>
#include <cmath>
#include <map>
#include <cstdio>
#include <string>
#include <cstring>
#include <string.h>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <stack>
#include <queue>
#include <ctype.h>
#include <vector>
#include <random>
#include<list> 
#define ll long long
#define ull unsigned long long
#define pb push_back
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define pii pair<int, int>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define pll pair<ll, ll>
#define lowbit(x) ((x)&(-x))
ll extend_gcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll d = extend_gcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}
ll fastpow(ll a, ll n, ll mod)
{
    ll ans = 1;
    a %= mod;
    while (n)
    {
        if (n & 1)
            ans = (ans * a) % mod; //% mod
        a = (a * a) % mod;         //% mod
        n >>= 1;
    }
    return ans;
}

inline void write(__int128 x)
{
    if (x > 9)
    {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}
__int128 sqrt(__int128 m)
{
    __int128 leftt = 0, rightt = ((__int128)1) << 51, ret = -1, mid;
    while (leftt < rightt)
    {
        mid = (leftt + rightt) / 2;
        if (mid * mid > m)
        {
            rightt = mid;
        }    
        else
        {
            leftt = mid + 1;
            ret = mid;
        }
    }
    return ret;
}

const double eps = 1e-6;
int sgn(double x)
{
    if(fabs(x)<eps)
    {
        return 0;
    }
    else return x<0?-1:1;
}

struct Point
{
    double x,y;
    Point()
    {

    }
    Point(double x,double y):x(x),y(y)
    {

    }
    Point operator + (Point B)
    {
        return Point(x+B.x,y+B.y);
    }
    Point operator - (Point B)
    {
        return Point(x-B.x,y-B.y);
    }
    bool operator == (Point B)
    {
        return sgn(x-B.x)==0&&sgn(y-B.y)==0;
    }
    bool operator < (Point B)
    {
        return sgn(x-B.x)<0||(sgn(x-B.x)==0&&sgn(y-B.y)<0);
    }
};
typedef Point Vector;
double Cross(Vector A,Vector B)//叉积
{
    return A.x*B.y-A.y*B.x;
}
double Distance(Point A,Point B)
{
    return hypot(A.x-B.x,A.y-B.y);
}
int Convex_hull(Point *p,int n,Point *ch)
{
    n=unique(p,p+n)-p;
    sort(p,p+n);
    int v=0;

    for(int i=0;i<n;i++)
    {
        while (v>1&&sgn(Cross(ch[v-1]-ch[v-2],p[i]-ch[v-1]))<=0)
        {
            v--;
        }
        ch[v++]=p[i];
    }

    int j=v;

    for(int i=n-2;i>=0;i--)
    {
        while (v>j&&sgn(Cross(ch[v-1]-ch[v-2],p[i]-ch[v-1]))<=0)
        {
            v--;
        }
        ch[v++]=p[i];
    }
    if(n>1)
    {
        v--;
    }
    return v;
}

int kmp(string s, string p)
{
    int ans = 0, lastt = -1;
    int lenp = p.size();
    vector<int > Next(lenp+3,0);
    rep(i, 1, lenp - 1)
    {
        int j = Next[i];
        while (j && p[j] != p[i])
        {
            j = Next[j];
        }
        if (p[j] == p[i])
        {
            Next[i + 1] = j + 1;
        }
        else
        {
            Next[i + 1] = 0;
        }
    }
    int lens = s.size();
    int j = 0;
    rep(i, 0, lens - 1)
    {
        while (j && s[i] != p[j])
        {
            j = Next[j];
        }
        if (s[i] == p[j])
        {
            j++;
        }
        if (j == lenp)
        {
            ans++;
        }
    }
    return ans;
}

int dir[4][2] =
    {
        {-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 左右上下
// int dir[8][2]={
//         {-1, 0}, {0, 1}, {1, 0}, {0, -1},{-1,-1},{-1,1},{1,-1},{1,1}
// };

//#define endl '\n'//交互题请删除本行
const ll inf = 1000000000000000000ll;
const ll mod1 = 998244353ll, P1 = 131, mod2 = 1e9 + 7ll, P2 = 13331;
ll inverse(ll x)
{
    return fastpow(x,mod1-2,mod1);
}

const int N = 2e5 + 10, M = 1e6 + 10;

///////////////////////////////////

int tt;

int n;

bool vis[N];

vector<int > v[N];

int d[N],ans,maxnum;

int numpoint;

///////////////////////////////////

void init_vis()
{
    maxnum=1e9;
    fill(vis+1,vis+n+1,0);
}

int query(int x,int y)
{
    cout<<"? "<<x<<" "<<y<<endl;
    int ret;
    cin>>ret;
    return ret;
}

void print(int x)
{
    cout<<"! "<<x<<endl;
}

void dfs(int u)
{
    d[u]=1;
    vis[u]=1;
    int tmp=0;
    for(auto i:v[u])
    {
        if(vis[i])
        {
            continue;
        }
        dfs(i);
        d[u]+=d[i];
        tmp=max(tmp,d[i]);
    }
    tmp=max(tmp,numpoint-d[u]);

    //
    //cout<<"dian"<<u<<" "<<tmp<<endl;

    if(tmp<maxnum)
    {
        maxnum=tmp;
        ans=u;
    }
}


void predfs(int u)
{
    vis[u]=1;
    numpoint++;
    for(auto i:v[u])
    {
        if(vis[i])
        {
            continue;
        }
        predfs(i);
    }
}

///////////////////////////////////



void init()
{
    rep(i,1,n)
    {
        v[i].clear();
    }
}

///////////////////////////////////

int main()
{
    //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//交互题请删除本行
    //freopen("ain.txt", "r", stdin); freopen("aout.txt", "w", stdout);
    
    cin>>tt;
    while (tt--)
    {
        cin>>n;

        init();

        rep(i,1,n)
        {
            int x,y;
            cin>>x>>y;
            if(x)
            {
                v[x].pb(i);
                v[i].pb(x);
            }
            if(y)
            {
                v[y].pb(i);
                v[i].pb(y);
            }
        }

        int point=1;

        rep(i,1,log2(n))
        {
            init_vis();
            numpoint=0;
            predfs(point);
            //
           // cout<<"dianshu"<<numpoint<<endl;
            init_vis();
            dfs(point);
          //  cout<<point<<" "<<ans<<endl;
            
         //   cout<<endl;
            //

            if(v[ans].size()==0)
            {
                print(ans);
                break;
            }

            if(v[ans].size()==1)
            {
                int ret=query(ans,v[ans][0]);
                if(ret==0)
                {
                    print(ans);
                }
                else
                {
                    print(v[ans][0]);
                }
                break;
            }

            if(v[ans].size()<2)
            {
                exit(0);
            }
            int ret=query(v[ans][0],v[ans][1]);
            if(ret==0)
            {
                point=v[ans][0];

                vector<int > vtemp;
                for(auto i:v[point])
                {
                    if(i!=ans)
                    {
                        vtemp.pb(i);
                    }
                }
                v[point]=vtemp;

            }
            else if(ret==1)
            {
                point=ans;
                int x=v[ans][0],y=v[ans][1];
                vector<int > vtemp;
                for(auto i:v[point])
                {
                    if(i!=x&&i!=y)
                    {
                        vtemp.pb(i);
                    }
                }
                v[point]=vtemp;
            }
            else
            {
                point=v[ans][1];
                vector<int > vtemp;
                for(auto i:v[point])
                {
                    if(i!=ans)
                    {
                        vtemp.pb(i);
                    }
                }
                v[point]=vtemp;
            }

        }
        

    }
    
    


    return 0;
}

详细

Test #1:

score: 0
Time Limit Exceeded

input:

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

output:

? 1 5
? 2 4

result: