QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#801589 | #9570. Binary Tree | qlwpc | TL | 0ms | 14084kb | C++14 | 3.1kb | 2024-12-07 03:13:16 | 2024-12-07 03:13:17 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cassert>
#include<cmath>
using ll = long long;
using namespace std;
const int N = 400010;
const int M = 801040;
const int K = 510;
const int lgN = 21;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
int read(){
int x=0,flag=1;char c;
while((c=getchar())<'0' || c>'9') if(c=='-') flag=-1;
while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
return x*flag;
}
struct node{
int to,next;
}q[M];
int head[N],ss,du[N],fa[N];int ls[N],rs[N];
int sz[N],mxs[N],Rt, curN;bool vis[N];
void addedge(int x,int y){
++du[x];++du[y];
q[++ss]=(node){y,head[x]};head[x]=ss;
q[++ss]=(node){x,head[y]};head[y]=ss;
}
int query(int x,int y)
{
printf("? %d %d\n",x,y);
fflush(stdout);
return read();
}
void dfs1(int x,int fa)
{//printf("x=%d\n",x);
sz[x]=mxs[x]=1;
for (int j=head[x];j;j=q[j].next)
{
int t=q[j].to;
if (t==fa||vis[t]) continue;
dfs1(t,x);
sz[x]+=sz[t];
mxs[x]=max(mxs[x], sz[t]);
}
mxs[x]=max(mxs[x], curN-sz[x]);
if (mxs[x]<mxs[Rt]) Rt=x;
}
int ans;
void solve(int x)
{
if (curN==1) {ans=x;return;}
dfs1(x, 0);
vis[x]=true;
int u=0,v=0,z=0,nxt;
for (int j=head[x];j;j=q[j].next)
{
int t=q[j].to;
if (vis[t]) continue;
if (u==0) u=q[j].to;
else if (v==0) v=q[j].to;
else if (z==0) z=q[j].to;
}
if (z>0)
{
int dir=query(ls[x],rs[x]);
if (dir==1){
vis[ls[x]]=vis[rs[x]]=true;
vis[x]=false;
nxt=fa[x];
curN=sz[fa[x]]+1;
}else
{
if (dir==0) nxt=ls[x];
else nxt=rs[x];
curN=sz[nxt];
}
}else// du==2
{
if (v==0) v=x;
int dir=query(u,v);
if (dir==1){
ans=x;
return;
}else
{
if (dir==0) nxt=u;
else nxt=v;
if (du[x]<2)
{
if (nxt==x) {ans=x;return;}
vis[u]=vis[v]=true;
vis[nxt]=false;
}
curN=sz[nxt];
}
}
Rt=0;
dfs1(nxt,0);
solve(Rt);
}
int main()
{
int T=read();
while(T--)
{
int n=read();
for (int i=1;i<=n;++i)
{
int x=read(),y=read();
ls[i]=x;rs[i]=y;
if (x) addedge(i,x),fa[x]=i;
if (y) addedge(i,y),fa[y]=i;
}
if (n==2)
{
int x=query(1,2);
printf("! %d\n",x==0?1:2);
fflush(stdout);
}else
{
curN=n;mxs[0]=INF;Rt=0;
dfs1(1,0);
//printf("crush");
solve(Rt);
printf("! %d\n",ans);
fflush(stdout);
}
ss=0;
for (int i=1;i<=n;++i) head[i]=du[i]=vis[i]=0;
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 14084kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 1 2 0 2 0 0 2
output:
? 1 5 ? 4 2 ! 3 ? 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 1 2 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 1 2
output:
? 5 4 ? 8 6 ? 6 7 ! 6 ? 3 5 ? 1 4 ? 3 2 ? 0 3