QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#721675#9570. Binary Treetablemoon#WA 12ms5644kbC++202.3kb2024-11-07 16:35:082024-11-07 16:35:08

Judging History

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

  • [2024-11-07 16:35:08]
  • 评测
  • 测评结果:WA
  • 用时:12ms
  • 内存:5644kb
  • [2024-11-07 16:35:08]
  • 提交

answer

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
constexpr int N(1e5+5);
vector<int>G[N];
int n,siz[N],centroid,maxw[N],S;
bool vis[N];
inline const void getSize(const int &u,const int &fa)
{
    siz[u]=1;
    for (const int &v:G[u])
        if (!vis[v]&&fa!=v)
            getSize(v,u),
            siz[u]+=siz[v];
}
inline const void getCentroid(const int &u,const int &fa)
{
    maxw[u]=0;
    for (const int &v:G[u])
        if (!vis[v]&&v!=fa)
            getCentroid(v,u),
            maxw[u]=max(maxw[u],siz[v]);
    maxw[u]=max(maxw[u],S-siz[u]);
    if (maxw[u]<maxw[centroid])centroid=u;
}
inline const void getCentroid(const int &u)
{
    getSize(u,0);
    maxw[centroid=0]=S=siz[u];
    getCentroid(u,0);
    getSize(centroid,0);
}
inline const bool cmp(const int &a,const int &b)
{
    return siz[a]>siz[b];
}
inline const void work()
{
    cin>>n;
    for (int lson,rson,i(1);i<=n;i++)
    {
        cin>>lson>>rson;
        if (lson)G[i].push_back(lson),G[lson].push_back(i);
        if (rson)G[i].push_back(rson),G[rson].push_back(i);
    }
    int root(1),f;
    while (1)
    {
        getCentroid(root);
        vector<int>adj;
        for (const int &p:G[centroid])if(!vis[p])adj.push_back(p);
        sort(adj.begin(),adj.end());
        if (S==1)
        {
            cout<<"! "<<centroid<<endl;
            break;
        }
        if (S==2)
        {
            cout<<"? "<<centroid<<" "<<adj.front()<<endl;
            cin>>f;
            cout<<"! "<<(f?adj.front():centroid)<<endl;
            break;
        }
        if (adj.size()==2)
        {
            cout<<"? "<<adj.front()<<" "<<adj.back()<<endl;
            cin>>f;
            if (f==1){cout<<"! "<<centroid<<endl;break;}
            vis[centroid]=1;
            root=f?adj.back():adj.front();
            continue;
        }
        cout<<"? "<<adj.front()<<" "<<adj[1]<<endl;
        cin>>f;
        if (f==1)
        {
            vis[adj.front()]=vis[adj[1]]=1;
            root=centroid;
            continue;
        }
        vis[centroid]=1;
        root=f?adj[1]:adj.front();
    }
    for (int i(1);i<=n;i++)vis[i]=0,G[i].clear();
}
int main()
{
    int T;cin>>T;
    while (T--)work();
    return 0;
}

详细

Test #1:

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

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 12ms
memory: 3488kb

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

result:

wrong answer Too many queries (test case 44)