QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#752068 | #9570. Binary Tree | asuna241631 | ML | 1ms | 5820kb | C++14 | 2.3kb | 2024-11-15 21:52:52 | 2024-11-15 21:52:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10,inf=1e9;
int e[N],h[N],nxt[N],d[N],n,cnt=1,rt,siz[N],mx[N],Tsiz;
void add(int x,int y){
cnt++;
e[cnt]=y;
d[y]++;
nxt[cnt]=h[x];
h[x]=cnt;
}
void dfs(int x,int f){
siz[x]=1;mx[x]=0;
for(int i=h[x];i;i=nxt[i]){
int y=e[i];
if(y==f||y==0) continue;
dfs(y,x);
mx[x]=max(mx[x],siz[y]);
siz[x]+=siz[y];
}
mx[x]=max(mx[x],Tsiz-siz[x]);
if(mx[x]<mx[rt]) rt=x;
}
void work(int x,int f){
siz[x]=1;
for(int i=h[x];i;i=nxt[i]){
int y=e[i];
if(y==f||y==0) continue;
work(y,x);
siz[x]+=siz[y];
}
}
int ask(int x,int y){
cout<<"?"<<" "<<x<<" "<<y<<endl;
int nnn;
cin>>nnn;
// cout<<nnn<<endl;
return nnn;
}
bool cmp(int x,int y){
return siz[x]<siz[y];
}
void found(int x,int s){
rt=0,mx[rt]=inf;Tsiz=s;
// cout<<x<<" "<<s<<endl;
dfs(x,0);
// cout<<x<<" "<<rt<<" "<<d[rt]<<endl;
if(d[rt]==0) cout<<"! "<<rt<<endl;
else if(d[rt]==1){
// puts("one");
// cout<<rt<<endl;
int y=e[h[rt]],ans=ask(rt,y);
if(ans==0) cout<<"! "<<rt<<endl;
else cout<<"! "<<y<<endl;
return ;
}else if(d[rt]==2){
// puts("two");
int son[3],ct=0;
for(int i=h[rt];i;i=nxt[i]){
int y=e[i];
if(y==0) continue;
e[i^1]=0,d[y]--;
son[++ct]=y;
}
work(rt,0);
int ans=ask(son[1],son[2]);
if(ans==1) {
cout<<"! "<<rt<<endl;
return ;
}else if(ans==0){
found(son[1],siz[son[1]]);
}else {
found(son[2],siz[son[2]]);
}
}else {
// puts("three");
int son[4],ct=0;
son[0]=son[1]=son[2]=son[3]=0;
for(int i=h[rt];i;i=nxt[i]){
int y=e[i];
if(y==0) continue;
e[i^1]=0,d[y]--;
son[++ct]=y;
}
work(rt,0);
sort(son+1,son+4,cmp);
int ans=ask(son[2],son[3]);
if(ans==0){
found(son[2],siz[son[2]]);
}else if(ans==1){
h[rt]=0,d[rt]=0;
add(rt,son[1]);add(son[1],rt);
found(son[1],siz[son[1]]+1);
}else {
found(son[3],siz[son[3]]);
}
}
return ;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=1;i<=n;i++) d[i]=h[i]=0;cnt=1;
for(int i=1;i<=n;i++){
int x,y;
scanf("%d%d",&x,&y);
if(x!=0) add(i,x),add(x,i);
if(y!=0) add(i,y),add(y,i);
}
found(1,n);
for(int i=1;i<=cnt;i++) e[i]=nxt[i]=0;
}
return 0;
}
/*
1
3
3 0
1 0
0 0
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5820kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 0 2 0 2 0 0 2
output:
? 1 3 ? 4 3 ! 4 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Memory Limit Exceeded
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 2 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 2 2 0 5 4 5 3 1 0 0 0 0 0 0 1 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 1 1 5 3 0 5 1 0 0 0 0 4 0 0 2 5 5 0 0 0 0 0 3 0 2 4 2 0 3 3 0 1 0 0 0 2 2 2 0 0 0 2 3 2 3 0 0 0 0 1 10 2 8 9 7 0 0 ...
output:
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