QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#730004#9570. Binary Treeucup-team3282TL 1ms7752kbC++141.9kb2024-11-09 18:16:132024-11-09 18:16:14

Judging History

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

  • [2024-11-09 18:16:14]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:7752kb
  • [2024-11-09 18:16:13]
  • 提交

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);
			}
			
			if (fa[R] == L)
				break;
			
			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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Time Limit Exceeded

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

output:

? 3 7
? 1 7
? 2 1
! 1
? 6 4
? 3 4
? 1 2
! 1
? 2 7
? 5 7
? 1 5
! 1
? 3 5
? 4 5
! 4
? 8 2
? 4 1
? 3 5
! 5
? 4 3
? 5 4
! 5
? 3 2
? 1 2
! 2
? 2 3
! 3
? 2 1
! 1
? 3 2
! 2
? 3 10
? 5 3
? 4 3
! 4
? 2 1
! 1
? 8 6
? 5 8
? 4 3
! 4
? 2 9
? 5 9
? 1 5
! 5
? 5 7
? 4 5
? 6 5
! 5
? 2 1
! 1
? 2 7
? 1 7
! 1
? 3 7
? 1...

result: