QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#729999#9570. Binary Treeucup-team3282WA 4ms7812kbC++141.9kb2024-11-09 18:14:542024-11-09 18:14:54

Judging History

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

  • [2024-11-09 18:14:54]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:7812kb
  • [2024-11-09 18:14:54]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN=5e5+5;
ll t,n;
struct edge{
	ll u,v,nxt;
}e[MAXN];
ll head[MAXN],edge_num;
void add_edge(ll From,ll To){
	e[++edge_num]=(edge){From,To,head[From]};
	head[From]=edge_num;
	return;
}
bool vis[MAXN];
ll deep[MAXN];
ll res;
ll fa[MAXN];
void solve(ll now,ll father){
	deep[now]=deep[father]+1;
	fa[now]=father;
	if(deep[now]>deep[res])res=now;
	for(int i=head[now];i;i=e[i].nxt){
		if(vis[e[i].v]||e[i].v==father)continue;
		solve(e[i].v,now);
	}
	return;
}
ll x,y;
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=1;i<=n;i++){
			cin>>x>>y;
			if(x){
				add_edge(i,x);
				add_edge(x,i);
			}
			if(y){
				add_edge(i,y);
				add_edge(y,i);
			}
		}
		ll L=1,R=n;
		res = 0;
		solve(1,1);
		L=res;
		res=0;
		solve(L,L);
		R=res;
		while(L!=R){
			ll tmp;
			cout<<"? "<<L<<" "<<R<<endl;
			cin>>tmp;
			
			if (tmp == 1)
			{
				
//				cerr << "!!!!!!!!!!" <<endl;
				
				int u = R;
				while (deep[u] * 2 != deep[L] + deep[R])
				{
					vis[u] = 1;
					u = fa[u];
					
					cerr << u <<endl;
				}
				vis[fa[u]] = 1;
				
//				cerr<<"done" << endl;
//				for (int i = 1; i <= n; i++)
//				cout << i <<" " << vis[i] <<endl;
//				exit(0);
				
				res = 0;
				solve(u, u);
				
//				cout << 99999 <<endl;
				L = res;
				res=0;
				solve(L, L);
				R = res;
				
//				cout<< 9933 <<endl;
				continue;
			}
			
			if (tmp == 2)
			{
				swap(L,R);
				res=0;
				solve(L,L);
			}
			
			int u = R;
			while (deep[u] - deep[L] > deep[R] - deep[u])
			{
				vis[u] = 1;
				u = fa[u];
			}
			R = u;
			
			res = 0;
			solve(L, L);
			L = res;
			res=0;
			solve(L, L);
			R = res;
		}
		
		cout << "! " << L <<endl;
		for(int i=1;i<=n;i++)vis[i]=head[i]=fa[i]=deep[i]=0;
		edge_num=0;
		//clear all array !
	}
	return 0;
}

詳細信息

Test #1:

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

input:

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

output:

? 4 5
? 1 5
! 2
? 2 1
! 1

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 4ms
memory: 7812kb

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
2
0
2
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
2
2
2

output:

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

result:

wrong answer Too many queries (test case 2)