QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#794396#9570. Binary TreefryanRE 0ms10860kbC++143.1kb2024-11-30 14:02:552024-11-30 14:02:55

Judging History

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

  • [2024-11-30 14:02:55]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:10860kb
  • [2024-11-30 14:02:55]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define int long long

const int mxn = 2e5;

int n,sub[mxn],vis[mxn];
vector<int> adj[mxn];

int siz;

int dfs(int v) {
	if (vis[v]) return 0;
	vis[v] = 1;
	sub[v] = 1;
	int mx = 0;
	int res = 0;
	for (int u : adj[v]) {
		if (!vis[u]) {
			res = max(res,dfs(u));
			sub[v] += sub[u];
			mx = max(mx,sub[u]);
		}
	}
	mx = max(mx,siz-sub[v]);
	if (siz/2 >= mx) res = v;
	return res;
}

void solve() {
	cin>>n;
	for (int i=1; i<=n; i++) {
		adj[i].clear();
	}
	for (int i=1; i<=n; i++) {
		int u,v; cin>>u>>v;
		if (u) adj[i].push_back(u);
		if (v) adj[i].push_back(v);
		if (u) adj[u].push_back(i);
		if (v) adj[v].push_back(i);
//		cout<<u<<" "<<v<<endl;
	}
	
	siz = n;
	int crt = 1;
	int iter = 0;
	while (siz > 1 && iter++<10) {
		for (int i=1; i<=n; i++) vis[i] = 0;
		int rt = dfs(crt);
//		cout<<rt<<": "<<siz<<endl;
//		for (auto i : adj[rt]) cout<<i<<" ";
//		cout<<"\n";
		if (sz(adj[rt]) == 3) {
			int u1 = adj[rt][0];
			int u2 = adj[rt][1];
			int u3 = adj[rt][2];
			
			int s1 = sub[u1]; if (s1 > sub[rt]) s1 = siz - sub[rt];
			int s2 = sub[u2]; if (s2 > sub[rt]) s2 = siz - sub[rt];
			int s3 = sub[u3]; if (s3 > sub[rt]) s3 = siz - sub[rt];
			
			int mins = min(s1,min(s2,s3));
			
			int v1,v2,v3;
			if (s3 == mins) {
				v1 = u1, v2 = u2, v3 = u3;
			} else if (s2 == mins) {
				v1 = u1, v2 = u3, v3 = u2;
			} else if (s1 == mins) {
				v1 = u2, v2 = u3, v3 = u1;
			}
			
			cout<<"? "<<v1<<" "<<v2<<endl;
//			cout<<v1<<" - "<<v2<<" - "<<v3<<" - "<<rt<<endl;
			int t; cin>>t;
			if (t==0) {
				crt = v1;
				adj[v1].erase(find(all(adj[v1]),rt));
				if (u1==v1) siz=s1;
				else if (u2==v1) siz = s2;
				else if (u3==v1) siz = s3;
			} else if (t==1) {
				crt = rt;
				adj[rt].erase(find(all(adj[rt]),v1));
				adj[rt].erase(find(all(adj[rt]),v2));
				if (u1==v3) siz=s1;
				else if (u2==v3) siz = s2;
				else if (u3==v3) siz = s3;
			} else if (t==2) {
				crt = v2;
				adj[v2].erase(find(all(adj[v2]),rt));
				if (u1==v2) siz=s1;
				else if (u2==v2) siz = s2;
				else if (u3==v2) siz = s3;
			}
		} else if (sz(adj[rt]) == 2) {
			int u1 = adj[rt][0]; 
			int u2 = adj[rt][1]; 
			
			int s1 = sub[u1]; if (s1 > sub[rt]) s1 = siz - sub[rt];
			int s2 = sub[u2]; if (s2 > sub[rt]) s2 = siz - sub[rt];
			
			cout<<"? "<<u1<<" "<<u2<<endl;
			int t; cin>>t;
			if (t==0) {
				crt = u1;
				adj[u1].erase(find(all(adj[u1]),rt));
				siz = s1;
			} else if (t==1) {
				crt = rt;
				siz = 1;
			} else if (t==2) {
				crt = u2;
				adj[u2].erase(find(all(adj[u2]),rt));
				siz = s2;
			}
		} else if (sz(adj[rt]) == 1) {
			int u1 = rt;
			int u2 = adj[rt][0];
			assert(siz==2);
			cout<<"? "<<u1<<" "<<u2<<endl;
			int t; cin>>t;
			if (t==0) {
				siz = 1;
				crt = u1;
			} else if (t==2) {
				siz = 1;
				crt = u2;
			} else {
				cout<<"WTF";
			}
		}
	}
	cout<<"! "<<crt<<endl;
}

signed main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
	
	int t; cin>>t;
	while (t--) {
		solve();
	}
	
}

详细

Test #1:

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

input:

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

output:

? 1 3
? 3 4
! 3
? 1 2
! 2

result:

ok OK (2 test cases)

Test #2:

score: -100
Runtime Error

input:

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

output:

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

result: