QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#747450#9570. Binary Treesilver_museTL 1ms3616kbC++202.7kb2024-11-14 17:12:042024-11-14 17:12:05

Judging History

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

  • [2024-11-14 17:12:05]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3616kb
  • [2024-11-14 17:12:04]
  • 提交

answer

#include<bits/stdc++.h>
// #define int long long
// #define endl '\n'
#define ph push_back
#define pp pop_back
using namespace std;

const int N=2e5+5,inf=1e9;
int T,n;
int sz[N],hs[N];
bool vis[N];
vector<int>eg[N],tg[N],node;

void clear()
{
    node.clear();
    for(int i=1;i<=n;i++) eg[i].clear();
    for(int i=1;i<=n;i++) vis[i]=1;
}
void dfs(int x,int fa)
{
    sz[x]=1; hs[x]=-1;
    for(auto it:eg[x])
        if(vis[it]&&it!=fa)
        {
            dfs(it,x);
            sz[x]+=sz[it];
            if(hs[x]==-1||sz[hs[x]]<sz[it]) hs[x]=it;
        }
}
void dfss(int x,int fa)
{
    vis[x]=0;
    for(auto it:eg[x])
        if(vis[it]&&it!=fa) dfss(it,x);
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>T;
    while(T--)
    {
        cin>>n;
        clear();
        for(int i=1,u,v;i<=n;i++)
        {
            cin>>u>>v;
            if(u) eg[i].ph(u),eg[u].ph(i);
            if(v) eg[i].ph(v),eg[v].ph(i);
            node.ph(i);
        }
        for(;;)
        {
            if(node.size()==1)
            {
                cout<<"! "<<node[0]<<endl;
                break;
            }
            dfs(node[0],0);
            int mn=inf,p,gg=node.size();
            for(auto it:node)
            {
                int t=hs[it]==-1?-1:sz[hs[it]];
                if(mn>max(t,gg-sz[it])) mn=max(t,gg-sz[it]),p=it;
            }
            // cout<<p<<endl;
            if(eg[p].size()==1)
            {
                int t;
                cout<<"? "<<p<<' '<<eg[p][0]<<endl;
                cin>>t;
                if(t==0) cout<<"! "<<p<<endl;
                else cout<<"! "<<eg[p][0]<<endl;
                break;
            }
            cout<<"? "<<eg[p][0]<<' '<<eg[p][1]<<endl;
            int t;
            cin>>t;
            if(t==0) 
            {
                dfss(eg[p][1],p);
                if(eg[p].size()==3) dfss(eg[p][2],p);
                vis[p]=0;
            }   
            else if(t==1)
            {
                dfss(eg[p][0],p);
                dfss(eg[p][1],p);
            }
            else if(t==2)
            {
                dfss(eg[p][0],p);
                if(eg[p].size()==3) dfss(eg[p][2],p);
                vis[p]=0;
            }
            node.clear();
            for(int i=1;i<=n;i++)
            {
                if(vis[i]) node.ph(i);
                tg[i].clear();
            }
            for(int it:node)
            {
                for(auto u:eg[it])
                    if(vis[u]) tg[it].ph(u);
            }
            for(int i=1;i<=n;i++) eg[i]=tg[i];
            // for(auto it:node) cout<<it<<' ';
            // cout<<endl;
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Time Limit Exceeded

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
2
2
0
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
2
2
0
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
2
1
2
5
4 5
3 1
0 0
0 0
0 0
1
0
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
0
2
5
3 0
5 1
0 0
0 0
4 0
0
0
5
5 0
0 0
0 0
3 0
2 4
1
2
3
3 0
1 0
0 0
2
2
2 0
0 0
2
3
2 3
0 0
0 0
2
10
2 8
9 7
0 0
...

output:

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

result: