QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#788469 | #9570. Binary Tree | JinTianHao | TL | 1ms | 3856kb | C++17 | 2.3kb | 2024-11-27 17:04:49 | 2024-11-27 17:04:53 |
Judging History
answer
#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
// #define int long long
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
using namespace std;
template <typename T> void read(T &x){
x=0;char ch=getchar();int fh=1;
while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
x*=fh;
}
template <typename T> void write(T x) {
if (x<0) x=-x,putchar('-');
if (x>9) write(x/10);
putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
write(x);
puts("");
}
int n;
int h[100005],e[200005],ne[200005],idx;
bool cut[200005];
vector<int> vis;
void add(int a,int b)
{
e[idx]=b,cut[idx]=0,ne[idx]=h[a],h[a]=idx++;
}
int rt,mn,all;
int siz[100005];
void dfs(int x,int fa)
{
vis.push_back(x);
siz[x]=1;
for(int i=h[x];~i;i=ne[i])
{
int j=e[i];
if(i==fa||cut[i]) continue;
dfs(j,i^1);
siz[x]+=siz[j];
if(mn>max(siz[j],all-siz[j]))
mn=max(siz[j],all-siz[j]),rt=i;
}
}
int ask(int u,int v)
{
printf("? %d %d\n",u,v);
fflush(stdout);
int ret;
read(ret);
return ret;
}
int solve()
{
if(all==1) return e[rt];
if(all==2)
{
if(ask(e[rt],e[rt^1])) rt^=1;
return e[rt];
}
if(all==3)
{
sort(vis.begin(),vis.end(),[](int a,int b){return siz[a]<siz[b];});
if(siz[vis[1]]==2)
{
int r=ask(vis[0],vis[2]);
if(r==0) return vis[0];
if(r==1) return vis[1];
if(r==2) return vis[2];
}
else
{
int r=ask(vis[0],vis[1]);
if(r==0) return vis[0];
if(r==1) return vis[2];
if(r==2) return vis[1];
}
}
if(ask(e[rt],e[rt^1]))
all=all-siz[e[rt]],mn=all,rt^=1;
else
all=siz[e[rt]],mn=all;
cut[rt]=cut[rt^1]=1;vis.clear();
dfs(e[rt],-1);
return solve();
}
signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int tc;read(tc);
int tt=0;
while(tc--)
{
++tt;
read(n);idx=0;
if(tt==17) fprintf(stderr,"%d\n",n);
for(int i=1;i<=n;++i)
h[i]=-1;
for(int i=1;i<=n;++i)
{
int x,y;
read(x);read(y);
if(x) add(x,i),add(i,x);
if(y) add(i,y),add(y,i);
if(tt==17) fprintf(stderr,"%d %d\n",x,y);
}
rt=0;mn=all=n;vis.clear();
dfs(e[rt],-1);
printf("! %d\n",solve());
fflush(stdout);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3856kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 1 2 0 2 0 0 2
output:
? 3 2 ? 5 1 ! 2 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Time Limit Exceeded
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 2 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 2 0 5 4 5 3 1 0 0 0 0 0 0 2 1 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 2 2 2 5 3 0 5 1 0 0 0 0 4 0 2 1 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 0 3 2 3 0 0 0 0 0 10 2 8 9 7 0 ...
output:
? 8 2 ? 6 2 ? 7 6 ! 6 ? 7 3 ? 8 5 ? 7 5 ! 7 ? 8 1 ? 2 6 ? 8 4 ! 8 ? 2 1 ? 5 4 ! 1 ? 5 3 ? 7 3 ? 2 3 ! 3 ? 5 2 ? 3 2 ! 1 ? 4 5 ? 2 1 ! 1 ? 2 3 ! 3 ? 2 1 ! 2 ? 3 2 ! 3 ? 7 2 ? 9 2 ? 8 2 ! 8 ? 2 1 ! 1 ? 5 7 ? 2 9 ? 1 7 ! 7 ? 10 5 ? 7 3 ? 5 1 ! 1 ? 8 9 ? 3 8 ? 5 6 ! 5 ? 1 2 ! 1 ? 4 5 ? 3 5 ? 2 3