QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#727657#9570. Binary Treeucup-team5697#TL 1ms3868kbC++231.7kb2024-11-09 13:31:092024-11-09 13:31:09

Judging History

你现在查看的是最新测评结果

  • [2024-11-09 13:31:09]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3868kb
  • [2024-11-09 13:31:09]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;bool Mbe;
inline int Ask(int x,int y){printf("? %d %d\n",x,y);fflush(stdout);scanf("%d",&x);return x;}
inline void Rep(int x){printf("! %d\n",x);fflush(stdout);}
namespace MAOJUN{

const int N=1e5+5;
int n;
const int E=N<<1;
int tot=0,hd[N],to[E],nxt[E];
inline void Add(int u,int v){to[++tot]=v;nxt[tot]=hd[u];hd[u]=tot;}

int al,mn,wc,sz[N];bool vs[N];
void gwc(int u,int f){
	int mx=sz[u]=1;
	for(int i=hd[u];i;i=nxt[i])if(int v=to[i];v!=f&&!vs[v]){
		gwc(v,u);sz[u]+=sz[v];mx=max(mx,sz[v]);
	}
	mx=max(mx,n-sz[u]);if(mx<mn){mn=mx;wc=u;}
}
int dvd(int u){
	vector<int>sn;vs[u]=1;gwc(u,0);
	for(int i=hd[u];i;i=nxt[i])if(int v=to[i];!vs[v])sn.emplace_back(v);
	if(sn.size()==0)return u;
	else if(sn.size()==1)return Ask(u,sn[0])?sn[0]:u;
	else if(sn.size()==2){
		int x=Ask(sn[0],sn[1]);
		if(x==1)return u;
		int v=x?sn[1]:sn[0];
		al=mn=sz[v];gwc(v,u);
		return dvd(wc);
	}
	int x=Ask(sn[0],sn[1]);
	if(x==1){
		vs[u]=0;vs[sn[0]]=vs[sn[1]]=1;
		al=mn=sz[sn[2]]+1;gwc(u,0);return dvd(wc);
	}
	int v=x?sn[1]:sn[0];
	al=mn=sz[v];gwc(v,u);
	return dvd(wc);
}
inline void slv(){
	scanf("%d",&n);
	tot=0;for(int i=1;i<=n;i++)hd[i]=vs[i]=0;
	for(int i=1,u,v;i<=n;i++){
		scanf("%d%d",&u,&v);
		if(u){Add(i,u);Add(u,i);}
		if(v){Add(i,v);Add(v,i);}
	}
	al=mn=n;gwc(1,0);Rep(dvd(wc));
}
inline void main(){
	int T;scanf("%d",&T);while(T--)slv();
}

}bool Med;int main(){
#ifdef LOCAL
	freopen("1.in","r",stdin);
	freopen("data.out","w",stdout);
#endif
	MAOJUN::main();
#ifdef LOCAL
	fprintf(stderr,"%lfs\n",clock()/1000.);
	fprintf(stderr,"%lfMB\n",(&Mbe-&Med)/1024./1024);
#endif
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3868kb

input:

2
5
0 0
1 5
2 4
0 0
0 0
1
0
2
0 2
0 0
2

output:

? 3 5
? 2 1
! 2
? 2 1
! 1

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
1
0
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
0
0
0

output:

? 4 5
? 6 8
? 2 1
! 2
? 3 5
? 3 5
? 3 5
? 3 5

result: