QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#730333 | #9570. Binary Tree | ucup-team4074# | RE | 0ms | 0kb | C++20 | 2.6kb | 2024-11-09 19:48:52 | 2024-11-09 19:48:52 |
answer
#include<bits/stdc++.h>
//#define int long long
using namespace std;
void solve();
signed main() {
int t = 1;
cin >> t;
while (t--) solve();
}
const int N = 2e5 + 10;
const int maxn=100010;
vector<int>e[maxn];
int v[maxn];
int rt=0,rtmx=1e9,s[maxn],sum;
int n;
int dfs(int x,int fa)
{
int sum1=1;
for(int i=0;i<e[x].size();i++)
{
if(!v[e[x][i]]||e[x][i]==fa)continue;
sum1+=dfs(e[x][i],x);
}
return sum1;
}
void getroot(int x,int fa)
{
if(fa==0)
{
rt=0;
rtmx=1e9;
}
s[x]=1;
int mx=0;
for(int i=0;i<e[x].size();i++)
{
int y=e[x][i];
if(y==fa||v[y]==0)continue;
getroot(y,x);
s[x]+=s[y];
mx=max(mx,s[y]);
}
mx=max(mx,sum-s[x]);
if(mx<rtmx)
{
rt=x;
rtmx=mx;
}
}
int ask(int x,int y)
{
cout<<"? "<<x<<" "<<y<<"\n";
cout.flush();
int r;
cin>>r;
return r;
}
void solve() {
cin>>n;
sum=n;
for(int i=1;i<=n;i++)
{
e[i].clear();
v[i]=1;
s[i]=0;
}
for(int i=1;i<=n;i++)
{
int u,v;
cin>>u>>v;
if(u)
{
e[i].push_back(u);
e[u].push_back(i);
}
if(v)
{
e[v].push_back(i);
e[i].push_back(v);
}
}
getroot(1,0);
int id=1;
while((1<<id)<=n)
{
id++;
int x=0,y=0;
for(int i=0;i<e[rt].size();i++)
{
if(!v[e[rt][i]])continue;
if(!x)x=e[rt][i];
else if(!y)y=e[rt][i];
}
if(!x&&!y)
{
cout<<"! "<<rt<<"\n";
cout.flush();
return;
}
else if(!y)
{
if(ask(rt,x)==0)
{
cout<<"! "<<rt<<"\n";
cout.flush();
return;
}
else
{
cout<<"! "<<x<<"\n";
cout.flush();
return;
}
}
int vi=ask(x,y);
if(vi==0)
{
v[rt]=0;
sum=dfs(x,0);
getroot(x,0);
}
else if(vi==1)
{
v[x]=0;
v[y]=0;
sum=dfs(rt,0);
getroot(rt,0);
}
else
{
v[rt]=0;
sum=dfs(y,0);
getroot(y,0);
}
}
cout<<"! "<<0<<"\n";
cout.flush();
}
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 1 1
output:
? 1 5 ? 2 4 ! 0