QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#738247 | #9570. Binary Tree | EMTz | WA | 19ms | 7716kb | C++20 | 2.4kb | 2024-11-12 18:22:02 | 2024-11-12 18:22:13 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define lson k<<1
#define rson (k<<1)|1
#define debug cout<<666<<endl;
using namespace std;
const int N=1e6+5;
vector<int>E[N];
int siz[N];
int rt;
int ma[N];
int st[N];
int now=1;
bool cmp(int a,int b)
{
return siz[a]>siz[b];
}
int cnt=0;
void dfs(int u,int fa,int tot)
{
siz[u]=1;
ma[u]=0;
for(auto v:E[u])
{
if(v==fa||st[v]==1) continue;
dfs(v,u,tot);
siz[u]+=siz[v];
ma[u]=max(ma[u],siz[v]);
}
ma[u]=max(ma[u],tot-siz[u]);
if(ma[u]<ma[rt])
{
rt=u;
}
}
void dfs1(int u,int fa)
{
// cout<<u<<"\n";
vector<int>a;
st[u]=1;
for(auto v:E[u])
{
if(v==fa||st[v]==1) continue;
a.push_back(v);
}
if(a.size()==0)
{
cout<<"! "<<u<<"\n";
fflush(stdout);
}
++now;
if(a.size()==1)
{
cout<<"? "<<a[0]<<" "<<u<<"\n";
fflush(stdout);
cnt++;
int t;
cin>>t;
if(t==0)
{
cout<<"! "<<a[0]<<"\n";
}
else cout<<"! "<<u<<"\n";
fflush(stdout);
}
else if(a.size()==2)
{
cout<<"? "<<a[0]<<" "<<a[1]<<"\n";
fflush(stdout);
cnt++;
int t;
cin>>t;
if(t==0)
{
rt=0;
dfs(a[0],u,siz[a[0]]);
dfs1(rt,u);
}
else if(t==1)
{
cout<<"! "<<u<<"\n";
fflush(stdout);
}
else
{
rt=0;
dfs(a[1],u,siz[a[1]]);
dfs1(rt,u);
}
}
else if(a.size()==3)
{
// cout<<"&";
sort(a.begin(),a.end(),cmp);
cout<<"? "<<a[0]<<" "<<a[1]<<"\n";
fflush(stdout);
cnt++;
int t;
cin>>t;
if(t==0)
{
rt=0;
dfs(a[0],u,siz[a[0]]);
dfs1(rt,u);
}
else if(t==1)
{
rt=0;
st[u]=0;
st[a[0]]=1;
st[a[1]]=1;
dfs(a[2],u,siz[a[2]]+1);
// cout<<rt<<"\n";
dfs1(rt,fa);
}
else
{
rt=0;
dfs(a[1],u,siz[a[1]]);
dfs1(rt,u);
}
}
}
void vision()
{
now=0;
ma[0]=1e9;
int n;
cin>>n;
rt=0;
for(int i=0;i<=n;i++)
{
E[i].clear();
st[i]=0;
}
for(int i=1;i<=n;i++)
{
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);
}
}
dfs(1,0,n);
// cout<<rt<<"\n";
dfs(rt,0,n);
dfs1(rt,0);
if(cnt>log2(n))
{
int ok=1/0;
}
cnt=0;
return ;
}
signed main()
{
// ios_base::sync_with_stdio(false);
// cin.tie(nullptr);
// cout.tie(nullptr);
int _=1;
cin>>_;
while(_--){
vision();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 7644kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 1 ? 2 5 ! 2 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 19ms
memory: 7716kb
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 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 0 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 ? 5 3 ? 3 1 ? 2 4 ! 2 ? 1 6 ? 1 7 ? 5 1 ! 1 ? 2 4 ? 2 3 ! 3 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 5 4 ! 4 ? 4 1 ? 4 3 ! 4 ? 3 2 ! 2 ? 1 2 ! 1 ? 2 3 ! 3 ? 2 6 ? 1 9 ? 9 10 ! 10 ? 1 2 ! 1 ? 5 9 ? 5 4 ? 3 8 ! 3 ? 5 8 ? 5 7 ? 7 9 ! 7 ? 9 3 ? 9 1 ? 2 7 ! 2 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 2 3 ? ...
result:
wrong answer There are 7 candidates. (test case 5001)