QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#733395#9570. Binary Treeqinglu09WA 3ms3640kbC++233.0kb2024-11-10 18:34:592024-11-10 18:35:01

Judging History

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

  • [2024-11-10 18:35:01]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3640kb
  • [2024-11-10 18:34:59]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>pii;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
// #define endl '\n'
#define debug(x) cout<<#x<<": "<<x<<endl
const int N=2e5+10;
typedef pair<ll,ll>PLL;

int num=0;

ll n;
int ask(int x, int y)
{
    // if(++num>__lg(n)) exit(0);
    cout << "? " << x << ' ' << y << endl;
    cout.flush();
    cin >> x;
    return x;
}

void answer(int x)
{
    cout << "! " << x << endl;
    cout.flush();
}

vector<int>e[N];
int siz[N],del[N];
int sum,root,mxs;
int ce[2];
int getsum(int u,int fa)
{
    siz[u]=1;
    for(auto v:e[u])
    {
        if(v==fa||del[v]) continue;
        getsum(v,u);
        siz[u]+=siz[v];
    }
    return siz[u];
}

void getroot(int u,int fa)
{
    siz[u]=1;
    int s=0;
    for(auto v:e[u])
    {
        if(v==fa||del[v]) continue;
        getroot(v,u);
        siz[u]+=siz[v];
        s=max(s,siz[v]);
    }
    s=max(s,sum-siz[u]);

    if(s<=sum/2)
        ce[ce[0]!=0]=u;
}

void init(int u)
{
    sum=getsum(u,0);
    mxs=1000000000;
    ce[0]=ce[1]=0;
}

void solve()
{
    ++num;

    cin >> n;
    if(num==0)
    {
        cout<<n<<'*';
    }

    for(int i=1;i<=n;i++) e[i].resize(0),del[i]=0,siz[i]=0;
    sum=root=mxs=0;
    rep(i, 1, n)
    {
        int u, v;
        cin >> u >> v;
        if(num==90)
        {
            cout<<u<<'|'<<v<<'*';
        }

        if(u!=0)
        {
            e[i].push_back(u);
            e[u].push_back(i);
        }
        if(v!=0)
        {
            e[i].push_back(v);
            e[v].push_back(i);
        }
    }
    if(num==90)
        exit(0);
    root=1;
    while(1)
    {
        init(root);
        getroot(root,0);
        root=ce[0];
        int x=-1,y=-1;
        for(auto p:e[root])
        {
            if(!del[p])
            {
                if(x==-1) x=p;
                else if(y==-1) y=p;
            }
        }

        if(x==-1)
        {
            answer(root);
            return;
        }
        if(y==-1)
        {
            int ans=ask(x,root);
            if(ans==0)
            {
                answer(x);
                return;
            }
            if(ans == 2)
            {
                answer(root);
                return;
            }
        }
        int ans=ask(x,y);
        if(ans==0)
        {
            del[root]=1;
            del[y]=1;
            root=x;
        }
        if(ans==1)
        {
            del[x]=1;
            del[y]=1;
            root=root;
        }
        if(ans==2)
        {
            del[x]=1;
            del[root]=1;
            root=y;
        }
    }
}


int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
// #ifndef  ONLINEJUDGE
//     freopen("test.in","r",stdin);
//     freopen("test.out","w",stdout);
// #endif
    int T;
    cin>>T;
    while(T--)
    {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3640kb

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 3572kb

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
0
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
0
0
2
5
4 5
3 1
0 0
0 0
0 0
1
2
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
0
0
5
3 0
5 1
0 0
0 0
4 0
0
2
5
5 0
0 0
0 0
3 0
2 4
1
0
3
3 0
1 0
0 0
2
2
2 0
0 0
0
3
2 3
0 0
0 0
2
10
2 8
9 7
0 0
...

output:

? 2 5
? 2 7
? 2 1
! 1
? 5 3
? 1 4
? 2 3
! 2
? 1 6
? 1 7
? 1 5
! 5
? 4 5
? 3 1
! 1
? 5 6
? 1 4
! 1
? 5 1
? 5 4
! 4
? 1 2
? 3 5
! 3
? 3 2
! 2
? 1 2
! 1
? 2 3
! 3
? 2 6
? 1 9
? 9 10
! 10
? 1 2
! 1
? 5 9
? 4 8
? 3 5
! 3
? 5 8
? 7 1
? 3 5
! 3
? 3 4
? 1 7
? 8 2
! 2
? 1 2
! 2
? 4 3
? 1 7
! 7
? 4 9
? 2 3
? ...

result:

wrong answer Token parameter [name=op] equals to "0|0*6|1*2|4*5|0*0|0*7|0*0|0*", doesn't correspond to pattern "[?!]{1,1}" (test case 90)