QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#738262 | #9570. Binary Tree | EMTz | WA | 0ms | 7660kb | C++20 | 2.4kb | 2024-11-12 18:27:30 | 2024-11-12 18:27:31 |
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);
}
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(u,fa,siz[a[2]]+1);
// cout<<rt<<"\n";
dfs1(rt,u);
}
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;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 7660kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1
output:
? 3 1 ! 5
result:
wrong answer There are 2 candidates. (test case 1)