QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#727805#9570. Binary Treeucup-team4938#TL 0ms9980kbC++142.6kb2024-11-09 13:52:202024-11-09 13:52:21

Judging History

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

  • [2024-11-09 13:52:21]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:9980kb
  • [2024-11-09 13:52:20]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define mod 998244353ll
#define pii pair<int,int>
#define fi first
#define se second
#define mems(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define db double
using namespace std;
const int maxn=200010;
const int inf=1e18;
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch-48);ch=getchar();}
	return x*f;
}
bool Mbe;

int n;
int head[maxn],tot;
struct nd{
	int nxt,to;
}e[maxn<<1];
void add(int u,int v){e[++tot]={head[u],v};head[u]=tot;}
int siz[maxn],w[maxn],rt,sum;
bool vis[maxn];
void getrt(int u,int fa){
	siz[u]=1,w[u]=0;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;if(v==fa||vis[v])continue;
		getrt(v,u);
		siz[u]+=siz[v],w[u]=max(w[u],siz[v]);
	}
	w[u]=max(w[u],sum-siz[u]);
	if(w[u]<=sum/2)rt=u;
}
void dfs(int u,int fa){
	siz[u]=1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;if(v==fa||vis[v])continue;
		dfs(v,u);
		siz[u]+=siz[v];
	}
}
void sovle(int u){
	dfs(u,0);
	// cout<<u<<"\n";
	int v1=0,v2=0,v3=0;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;if(vis[v])continue;
		if(!v1)v1=v;
		else if(!v2)v2=v;
		else v3=v;
	}
	if(v3){
		printf("? %lld %lld\n",v1,v2);fflush(stdout);
		int x=read();
		if(x==0){
			int v=v1;vis[u]=1;
			sum=siz[v];getrt(v,u);sovle(rt);
			return ;
		}
		if(x==1){
			vis[v1]=vis[v2]=1;
			int v=u;sum=siz[v3]+1;
			getrt(u,v1);sovle(rt);
			return ;
		}
		if(x==2){
			int v=v2;vis[u]=1;
			sum=siz[v];getrt(v,u);sovle(rt);
			return ;
		}
	}
	if(v2){
		printf("? %lld %lld\n",v1,v2);fflush(stdout);
		int x=read();
		if(x==0){
			int v=v1;vis[u]=1;
			sum=siz[v];getrt(v,u);sovle(rt);
			return ;
		}
		if(x==1){
			printf("! %lld\n",u);fflush(stdout);
			return ;
		}
		if(x==2){
			int v=v2;vis[u]=1;
			sum=siz[v];getrt(v,u);sovle(rt);
			return ;
		}
	}
	if(v1){
		printf("? %lld %lld\n",v1,u);fflush(stdout);
		int x=read();
		if(x==0){
			printf("! %lld\n",v1);fflush(stdout);
			return ;
		}
		if(x==2){
			printf("! %lld\n",u);fflush(stdout);
			return ;
		}
	}
}
void work(){
	n=read();
	for(int i=1;i<=n;i++)head[i]=vis[i]=0;tot=0;
	for(int i=1;i<=n;i++){
		int u=read(),v=read();
		if(u)add(i,u),add(u,i);
		if(v)add(i,v),add(v,i);
	}
	sum=n;getrt(1,0);
	sovle(rt);
}

// \
444

bool Med;
int T;
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	
//	cerr<<(&Mbe-&Med)/1048576.0<<" MB\n";
	
	T=read();
	while(T--)work();
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 9980kb

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

output:

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

result: