QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#804512 | #9570. Binary Tree | qlwpc | TL | 3ms | 14108kb | C++14 | 3.2kb | 2024-12-07 23:26:39 | 2024-12-07 23:26:43 |
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);
//printf("Rt=%d curN=%d sz[2]=%d sz[3]=%d\n",Rt,curN,sz[2],sz[3]);
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 (sz[u]<sz[v]) swap(u,v);
if (sz[u]<sz[z]) swap(u,z);
if (sz[v]<sz[z]) swap(v,z);
if (z>0)
{
int dir=query(u,v);
if (dir==1){
vis[u]=vis[v]=true;
vis[x]=false;
nxt=z;
curN=sz[z]+1;
}else
{
if (dir==0) nxt=u;
else nxt=z;
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 (v==x)
{
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: 3ms
memory: 14108kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 5 ? 1 2 ! 1 ? 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 0 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 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 0 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 0 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 4 ? 2 7 ? 2 1 ! 1 ? 3 5 ? 3 4 ? 1 2 ! 1 ? 1 6 ? 1 7 ? 1 5 ! 5 ? 2 5 ? 2 3 ! 3 ? 5 6 ? 4 1 ! 4 ? 1 5 ? 1 3 ! 3 ? 4 2 ? 4 3 ! 4 ? 2 3 ! 3 ? 1 2 ! 1 ? 3 2 ! 2 ? 2 6 ? 9 1 ? 1 8 ! 8 ? 1 2 ! 1 ? 9 5 ? 9 6 ? 9 1 ! 9 ? 5 8 ? 7 1 ? 5 3 ! 5 ? 9 3 ? 9 7 ? 1 2 ! 1 ? 1 2 ! 2 ? 4 3 ? 7 1 ! 1 ? 4 9 ? 2 3 ? 2 ...