QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#730224 | #9570. Binary Tree | ucup-team4074# | RE | 0ms | 6560kb | C++17 | 2.6kb | 2024-11-09 19:18:33 | 2024-11-09 19:18:35 |
Judging History
answer
#include<bits/stdc++.h>
//#define int long long
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;
void solve();
signed main() {
// fast
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;
else 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 iid=0;
while(iid<20)
{
iid++;
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";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 6560kb
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 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Runtime Error
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 ? 1 2 ! 2 ? 5 3 ? 1 4 ? 3 2 ! 3 ? 1 6 ? 1 7 ? 5 1 ! 1 ? 4 5 ? 3 1 ! 1 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 4 5 ! 5 ? 1 2 ? 3 5 ! 3 ? 3 2 ! 2 ? 2 1 ! 2 ? 2 3 ! 3 ? 2 6 ? 1 9 ? 10 9 ! 9 ? 2 1 ! 2 ? 5 9 ? 4 8 ? 5 3 ! 5 ? 5 8 ? 7 1 ? 5 3 ! 5 ? 3 4 ? 1 7 ? 8 2 ! 2 ? 2 1 ! 1 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 2 3 ? 4...