QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#736493#9570. Binary Treeucup-team4967RE 0ms0kbC++202.9kb2024-11-12 11:23:482024-11-12 11:23:49

Judging History

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

  • [2024-11-12 11:23:49]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-12 11:23:48]
  • 提交

answer

#include "bits/stdc++.h"

using namespace std;

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define FORR(i, m, n) for(ll i = m;i >= n;i--)
#define REPO(i, n) for(ll i = 1;i <= n;i++)
#define ll long long
#define INF (ll)1ll << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(),n.end()
#define MOD (ll)1000000007
#define P pair<int, int>

int dfs_sz(int v, int p, vector<vector<int>> &g, vector<int> &sz, vector<int> &used) {
	sz[v] = 1;
	int idx = -1;
	REP(i, g[v].size()){
		int w = g[v][i];
		if(used[w] or w == p) continue;
		if(idx == -1) idx = i;
		sz[v] += dfs_sz(w, v, g, sz, used);
		if(sz[g[v][0]] < sz[w]) swap(g[v][0], w);
	}
	return sz[v];
}

P dfs_search(int v, int p, int nsz, vector<vector<int>> &g, vector<int> &sz, vector<int> &used){
	ll now = -1;
	for(auto& i: g[v]) {
		if(used[i] or i == p)continue;
		if(sz[i] > nsz / 2)return dfs_search(i, v, nsz, g, sz, used);
		if(sz[i] * 2 == nsz) now = i;
	}
	return P(v, now);
}

void dfs_used(int v, int p, vector<vector<int>> &g, vector<int> &used){
	used[v] = true;
	for(auto& i: g[v]) {
		if(i == p or used[i])continue;
		dfs_used(i, v, g, used);
	}
}

ll query(P p, vector<vector<int>> &g, vector<int> &sz, vector<int> &used){
	// 2
	if(p.second != -1){
		cout << "? " << p.first + 1 << " " << p.second + 1 << endl;
		int a;
		cin >> a;
		if(a == 0){
			dfs_used(p.second, p.first, g, used);
			return p.first;
		}
		else {
			dfs_used(p.first, p.second, g, used);
			return p.second;
		}
	}
	// 3
	int v = p.first;
	vector<int> h;
	for(auto& i: g[v]) {
		if(used[i])continue;
		h.push_back(i);
	}
	//3-0(goal)
	if(h.size() == 0) return v - 1000000;
	else if(h.size() == 1) { // 3-1
		return query(P(v, h[0]), g, sz, used);
	}
	else { // 3 - 2, 3
		cout << "? " << h[0] + 1 <<" " << h[1] + 1<< endl;
		int a;
		cin >> a;
		if(a == 0){ //h[0]
			dfs_used(v, h[0], g, used);
			return h[0];
		}
		else if(a == 1){
			dfs_used(h[0], v, g, used);
			dfs_used(h[1], v, g, used);
			return v;
		}
		else {//h1
			dfs_used(v, h[1], g, used);
			return h[1];
		}
	}
}

ll solve2(int v, vector<vector<int>> &g, vector<int> &sz, vector<int> &used){
	ll nsz = dfs_sz(v, -1, g, sz, used);
	P d = dfs_search(v, -1, nsz, g, sz, used);
	ll nx = query(d, g, sz, used);
	if(nx < 0) return nx + 1000000;
	return solve2(nx, g, sz, used);
}
void solve(ll n){
	vector<vector<int>> g(n + 10);
	vector<int> sz(n + 10), used(n + 10);
	REP(i, n){
		ll a, b;
		cin >> a >> b;
		a--;
		b--;
		if(a != -1) {
			g[i].push_back(a);
			g[a].push_back(i);
		}
		if(b != -1) {
			g[i].push_back(b);
			g[b].push_back(i);
		}
		//ok
	}
	ll res = solve2(0, g, sz, used);
	cout << "! " << res + 1 << endl;
}

int main() {
	ll tt;
	cin >> tt;
	REP(i, tt){
		ll n;
		cin >> n;
		solve(n);
	}
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

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

output:

? 3 5
! 2

result: