QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#732916 | #9570. Binary Tree | ucup-team4479# | WA | 5ms | 3804kb | C++23 | 2.3kb | 2024-11-10 16:26:39 | 2024-11-10 16:26:39 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
constexpr int N=200005;
int n;
vector<int>G[N];
int query(int x,int y)
{
cout<<"? "<<x<<" "<<y<<endl;
int t;
if(!(cin>>t)) exit(0);
return t;
}
void submit(int x)
{
cout<<"! "<<x<<endl;
return;
}
int siz[N];
int mx[N];
int rt,tot;
bool vis[N];
void findroot(int u,int father)
{
siz[u]=1;
mx[u]=0;
for(int v:G[u])
{
if(v==father) continue;
if(vis[v]) continue;
findroot(v,u);
siz[u]+=siz[v];
mx[u]=max(mx[u],siz[v]);
}
mx[u]=max(mx[u],tot-siz[u]);
if(rt==0||mx[u]<mx[rt]) rt=u;
return;
}
int p;
void dfs(int u)
{
vector<int>son;
for(int v:G[u])
{
if(vis[v]) continue;
son.emplace_back(v);
}
if((int)son.size()==0)
{
p=u;
return;
}
else if((int)son.size()==1)
{
int t=query(u,son[0]);
if(t==0) p=u;
else if(t==2)
{
vis[u]=true;
tot=siz[son[0]],rt=0;
findroot(son[0],0);
findroot(rt,0);
dfs(rt);
}
}
else
{
int t=query(son[0],son[1]);
if(t==0)
{
vis[u]=true;
tot=siz[son[0]],rt=0;
findroot(son[0],0);
findroot(rt,0);
dfs(rt);
}
else if(t==2)
{
vis[u]=true;
tot=siz[son[1]],rt=0;
findroot(son[1],0);
findroot(rt,0);
dfs(rt);
}
else
{
vis[son[0]]=vis[son[1]]=true;
tot-=siz[son[0]]+siz[son[1]],rt=0;
findroot(u,0);
findroot(rt,0);
dfs(rt);
}
}
return;
}
void solve()
{
cin>>n;
for(int i=1;i<=n;i++)
G[i].clear(),vis[i]=false;
for(int i=1;i<=n;i++)
{
int x,y;
cin>>x>>y;
if(x) G[i].emplace_back(x),G[x].emplace_back(i);
if(y) G[i].emplace_back(y),G[y].emplace_back(i);
}
tot=n,rt=0;
p=0;
findroot(1,0);
findroot(rt,0);
dfs(rt);
submit(p);
return;
}
int main()
{
ios::sync_with_stdio(false);
// cin.tie(nullptr),cout.tie(nullptr);
int T;
cin>>T;
while(T--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3804kb
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
Wrong Answer
time: 5ms
memory: 3648kb
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...
result:
wrong answer Too many queries (test case 90)