QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#733263 | #9570. Binary Tree | qinglu09# | RE | 0ms | 0kb | C++23 | 2.8kb | 2024-11-10 17:58:55 | 2024-11-10 17:58:56 |
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 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<mxs) mxs=s,root=u;
}
void init(int u)
{
sum=getsum(u,0);
mxs=0;
}
void solve()
{
num=0;
cin >> n;
for(int i=1;i<=n;i++) e[i].clear(),del[i]=0,siz[i]=0;
sum=root=mxs=0;
rep(i, 1, n)
{
int u, v;
cin >> 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);
}
}
root=1;
while(1)
{
init(root);
getroot(root,0);
cout.flush();
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: 0
Runtime Error
input:
2 5 0 0 1 5 2 4 0 0 0 0 0
output:
? 2 1 ! 2