QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#772133 | #9570. Binary Tree | Dixiky_215 | WA | 1ms | 17948kb | C++20 | 2.5kb | 2024-11-22 17:09:55 | 2024-11-22 17:09:56 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN=2e6+7;
int t,n;
int cnt=1,head[MAXN],to[MAXN],nxt[MAXN];
int siz[MAXN],now_sum=0,maxl[MAXN];
int tot=0,id[MAXN],fa[MAXN];
bool vis[MAXN];
bool edge[MAXN];
void add(int x,int y)
{
to[++cnt]=y;
nxt[cnt]=head[x];
head[x]=cnt;
return;
}
void dfs(int x)
{
id[++tot]=x;
vis[x]=1;siz[x]=1;maxl[x]=0;
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(vis[y]||edge[i]) continue;
fa[y]=x;
dfs(y);
siz[x]+=siz[y];
maxl[x]=max(maxl[x],siz[y]);
}
maxl[x]=max(maxl[x],now_sum-siz[x]);
return;
}
struct hhh
{
int s,id;
}c[MAXN];
bool cmp(hhh x,hhh y)
{
return x.s>y.s;
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);cout.tie(0);
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
{
int u=i,v;
cin>>v;
if(v) add(u,v),add(v,u);
cin>>v;
if(v) add(u,v),add(v,u);
}
now_sum=n;
int rot=1;
while(1)
{
tot=0;
dfs(rot);
int minl=1e9,min_id;
if(tot==1)
{
cout<<"! "<<id[1]<<endl;
break;
}
for(int i=1;i<=tot;i++)
{
int x=id[i];
if(maxl[x]<minl)
{
minl=maxl[x];
min_id=x;
}
}
int numk=0;
for(int i=head[min_id];i;i=nxt[i])
{
int y=to[i];
if(fa[min_id]==y||edge[i]) continue;
c[++numk]={siz[y],y};
}
if(min_id!=rot)
{
c[++numk]={now_sum-siz[min_id],fa[min_id]};
}
sort(c+1,c+1+numk,cmp);
if(numk>1)
{
cout<<"? "<<c[1].id<<" "<<c[2].id<<endl;
int sss;
cin>>sss;
if(sss==0)
{
int x=c[1].id;
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(y==min_id)
{
edge[i]=edge[i^1]=1;
break;
}
}
rot=x;
now_sum=siz[rot];
}
else if(sss==2)
{
int x=c[2].id;
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(y==min_id)
{
edge[i]=edge[i^1]=1;
break;
}
}
rot=x;
now_sum=siz[rot];
}
else
{
cout<<"! "<<min_id<<endl;
break;
}
}
else
{
cout<<"? "<<min_id<<" "<<c[1].id<<endl;
int sss;
cin>>sss;
if(sss==0) cout<<"! "<<min_id<<endl;
else cout<<"! "<<c[1].id<<endl;
break;
}
}
for(int i=0;i<=n;i++)
{
head[i]=vis[i]=siz[i]=0;
}
for(int i=0;i<=cnt;i++) nxt[i]=to[i]=edge[i]=0;
cnt=1;
}
return 0;
}
/*
1
10 2
2 5 2 1 10 3 2 9 9 6
17 9
*/
詳細信息
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 17948kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1
output:
? 3 5 ! 2
result:
wrong answer There are 2 candidates. (test case 1)