QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#788479 | #9570. Binary Tree | JinTianHao | TL | 1ms | 3788kb | C++17 | 2.4kb | 2024-11-27 17:07:25 | 2024-11-27 17:07:27 |
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,tt=0;read(tc);
if(tc<=3) tt=114514;
while(tc--)
{
++tt;
read(n);idx=0;
if(tt==17) printf("%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) printf("%d %d\n",x,y);
}
if(tt==17||tt>=114514)
{
rt=0;mn=all=n;vis.clear();
dfs(e[rt],-1);
printf("! %d\n",solve());
fflush(stdout);
}
else
{
printf("! 1\n");
fflush(stdout);
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3788kb
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
output:
! 1