QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#777150#9570. Binary TreeLanthanmumRE 1ms3796kbC++202.6kb2024-11-23 23:09:032024-11-23 23:09:04

Judging History

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

  • [2024-11-23 23:09:04]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3796kb
  • [2024-11-23 23:09:03]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N = 1e5 + 9;
int mxs, sum, rt, n;
int flag;
int del[N], sz[N];
vector<int> e[N];

auto ask(int u, int v) {
	cout << "?" << " " << u << " " << v << endl;
	int x;
	cin >> x;
	return x;
};

auto ok(int x) {
	cout << "!" << " " << x << endl;
};

void add(int u, int v) {
	e[u].push_back(v);
	e[v].push_back(u);
}
int get_size(int u,int f){
	if(del[u]){
		return 0;
	}
    int res=1;
    for(auto & i : e[u]){
        if(i==f)continue;
        res+=get_size(i,u);
    }
    return res;
}
void getroot(int u, int f) {
	if (del[u]) {
		sz[u] = 0;
	} else {
		sz[u] = 1;
	}
	int s = 0;
	for (auto &i : e[u]) {
		if (i == f || del[i]) {
			continue;
		}
		getroot(i, u);
		sz[u] += sz[i];
		s = max(s, sz[i]);
	}
	s = max(s, sum - sz[u]);
	if (s < mxs) {
		mxs = s;
		rt = u;
	}
}

void dfs(int rt) {
	if (flag) {
		return;
	}
	if (del[rt]) {
		return;
	}
	for (auto &i : e[rt]) {
		if (del[i]) {
			continue;
		}
		sum = sz[i];
		mxs = sz[i];
		getroot(i, 0);
	}
	vi t;
	for (auto &i : e[rt]) {
		if (!del[i]) {
			t.push_back(i);
		}
	}
	int SZ = t.size();
	if (!SZ) {
		ok(rt);
		flag = 1;
		return;
	} else if (SZ == 1) {
		int x = ask(rt, t[0]);
		if (!x) {
			ok(rt);
			flag = 1;
			return;
		} else {
			ok(t[0]);
			flag=1;
			return;
		}
	} else if (SZ == 2) {
		int x = ask(t[0], t[1]);
		if (x == 1) {
			ok(rt);
			flag = 1;
			return;
		}
		del[rt] = 1;
		if (!x) {
			dfs(t[0]);
			return;
		}
		dfs(t[1]);
		return;
	} else {
		vi szz(3);
		vector<array<int,2> > f;
		for(int i=0;i<3;i++){
			f.push_back({t[i],get_size(t[i],rt)});
		}
		sort(f.begin(),f.end(),[&](const array<int,2> &x,const array<int,2> &y){
            return x[1]>y[1];
        });
        for(int i=0;i<3;i++){
        	t[i]=f[i][0];
        }
		int x = ask(t[0], t[1]);
		if (x == 0) {
			del[rt] = 1;
			dfs(t[0]);
			return;
		} else if (x == 1) {
			del[t[0]] = del[t[1]] = 1;
			dfs(rt);
			return;
		} else {
			del[rt] = 1;
			dfs(t[1]);
			return;
		}
	}
}

void solve() {
	cin >> n;
	for(int i=1;i<=n;i++){
		e[i].clear();
		sz[i]=0,del[i]=0;
	}
	for (int i = 1; i <= n; i++) {
		int u, v;
		cin >> u >> v;
		if (u) {
			add(u, i);
		}
		if (v) {
			add(v, i);
		}
	}
	mxs = n, sum = n;
	getroot(1,0);
	dfs(rt);
};

int main() {
	int t;
	cin >> t;
	while (t--) {
		flag = 0;
		solve();
	}
	return 0;
}

詳細信息

Test #1:

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

input:

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

output:

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

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

output:

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

result: