QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#750231 | #9570. Binary Tree | UESTC_DECAYALI# | WA | 4ms | 5856kb | C++20 | 2.0kb | 2024-11-15 13:33:51 | 2024-11-15 13:33:51 |
Judging History
answer
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#define RI register int
#define CI const int&
using namespace std;
const int N=100005;
int t,n,x,y,sz[N],mx[N],vis[N],rt,ots; vector <int> v[N];
inline int ask(CI u,CI v)
{
printf("? %d %d\n",u,v); fflush(stdout);
int res; scanf("%d",&res); return res;
}
inline void answer(CI x)
{
printf("! %d\n",x); fflush(stdout);
}
inline void getrt(CI now=1,CI fa=0)
{
sz[now]=1; mx[now]=0;
for (auto to:v[now]) if (to!=fa&&!vis[to])
{
getrt(to,now); sz[now]+=sz[to];
mx[now]=max(mx[now],sz[to]);
}
mx[now]=max(mx[now],ots-sz[now]);
if (mx[now]<mx[rt]) rt=now;
}
inline void solve(int now)
{
vector <int> son;
for (auto to:v[now]) if (!vis[to]) son.push_back(to);
if (son.empty()) return answer(now);
if (son.size()==1)
{
int res=ask(now,son.back());
if (res==0) answer(now); else answer(son.back());
return;
}
if (son.size()==2)
{
int res=ask(son[0],son[1]);
if (res==0)
{
vis[now]=1; mx[rt=0]=ots=sz[son[0]];
getrt(son[0],now); getrt(rt); solve(rt);
} else
if (res==2)
{
vis[now]=1; mx[rt=0]=ots=sz[son[1]];
getrt(son[1],now); getrt(rt); solve(rt);
} else answer(now);
return;
}
auto cmp=[&](CI x,CI y)
{
return sz[x]>sz[y];
};
sort(son.begin(),son.end(),cmp);
int res=ask(son[0],son[1]);
if (res==0)
{
vis[now]=1; mx[rt=0]=ots=sz[son[0]];
getrt(son[0],now); getrt(rt); solve(rt);
} else
if (res==2)
{
vis[now]=1; mx[rt=0]=ots=sz[son[1]];
getrt(son[1],now); getrt(rt); solve(rt);
} else
{
vis[son[0]]=vis[son[1]]=1; getrt(now);
mx[rt=0]=ots=sz[now];
getrt(now); getrt(rt); solve(rt);
}
return;
}
int main()
{
for (scanf("%d",&t);t;--t)
{
scanf("%d",&n);
for (RI i=1;i<=n;++i) vis[i]=0,v[i].clear();
for (RI i=1;i<=n;++i)
{
scanf("%d%d",&x,&y);
if (x!=0) v[i].push_back(x),v[x].push_back(i);
if (y!=0) v[i].push_back(y),v[y].push_back(i);
}
mx[rt=0]=ots=n; getrt(); solve(rt);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3792kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 0 2 0 2 0 0 2
output:
? 1 3 ? 4 3 ! 4 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 4ms
memory: 5856kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 2 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 1 2 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 2 2 5 4 5 3 1 0 0 0 0 0 0 0 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 2 5 3 0 5 1 0 0 0 0 4 0 0 0 5 5 0 0 0 0 0 3 0 2 4 2 2 3 3 0 1 0 0 0 2 2 2 0 0 0 2 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 2 4 ? 1 6 ? 7 6 ! 7 ? 5 3 ? 1 4 ? 3 2 ! 2 ? 1 6 ? 5 3 ? 7 3 ! 3 ? 2 4 ? 3 2 ! 3 ? 5 6 ? 1 4 ! 4 ? 5 1 ? 4 5 ! 4 ? 1 4 ? 3 4 ! 4 ? 3 2 ! 2 ? 2 1 ! 1 ? 2 3 ! 3 ? 2 6 ? 1 9 ? 8 1 ! 8 ? 2 1 ! 2 ? 5 9 ? 2 1 ? 6 2 ! 6 ? 5 8 ? 7 1 ? 9 7 ! 7 ? 9 3 ? 1 7 ? 9 2 ! 2 ? 2 1 ! 1 ? 4 3 ? 1 7 ! 7 ? 9 4 ? 2 3 ? 4 ...
result:
wrong answer There are 2 candidates. (test case 20)