QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#729568#9570. Binary Treeucup-team4975TL 0ms0kbC++233.6kb2024-11-09 17:23:072024-11-09 17:23:08

Judging History

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

  • [2024-11-09 17:23:08]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-09 17:23:07]
  • 提交

answer

#define LOCAL
#include <bits/stdc++.h>
#define fir first
#define sec second
#define el '\n'

#ifdef LOCAL
#define FINISH cerr << "FINISH" << endl;
#else
#define FINISH ;
#endif

#ifdef LOCAL
#define debug(x) cerr << setw(4) << #x << " == " << x << endl
#else
#define debug(x)
#endif

#ifdef LOCAL
#define debugv(x)                   \
	cerr << setw(4) << #x << ":: "; \
	for (auto i : x)                \
		cerr << i << " ";           \
	cerr << endl
#else
#define debugv(x)
#endif

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
ostream& operator<<(ostream& out, PII& x)
{
	out << x.fir << " " << x.sec << endl;
	return out;
}

const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int N = 200020;
void solve()
{
	int n;
	cin >> n;
	vector<vector<int>> edge(n + 1);
	for (int i = 1; i <= n; i++) {
		int x, y;
		cin >> x >> y;
		if (x != 0) {
			edge[i].push_back(x);
			edge[x].push_back(i);
		}
		if (y != 0) {
			edge[y].push_back(i);
			edge[i].push_back(y);
		}
	}
	int rt = 1, lft = n;

	if (n == 1) {
		cout << "! " << n << endl;
		return;
	}
	// FINISH
	while (1) {
		vector<int> siz(n + 1, 0);

		int newrt = -1, s = inf;
		vector<int> w(n + 1, 0);
		w[newrt = 0] = inf;
		auto dfs = [&](auto& self, int x, int fa) -> void {
			/*debug(x);
			debug(fa);*/

			siz[x] = 1;
			for (auto to : edge[x]) {
				if (to == fa)
					continue;
				self(self, to, x);
				siz[x] += siz[to];
				w[x] = max(w[x], siz[to]);
			}
			w[x] = max(w[x], lft - siz[x]);
			if (w[x] < w[newrt])
				newrt = x;
		};

		// cout << rt << " " << lft << endl;
		dfs(dfs, rt, rt);

		// debug(newrt);

		int q1 = 0, q2 = 0;
		if (edge[newrt].size() == 0) {
			cout << "! " << newrt << endl;
			return;
		}

		if (edge[newrt].size() == 1) {
			q1 = newrt, q2 = edge[newrt][0];
			cout << "? " << q1 << " " << q2 << endl;
			int x;
			cin >> x;
			if (x == 0) {
				cout << "! " << q1 << endl;
			}
			if (x == 2) {
				cout << "! " << q2 << endl;
			}
			return;
		}

		if (edge[newrt].size() == 3) {
			for (auto to : edge[newrt]) {
				if (siz[to] < siz[newrt])
					w[to] = siz[to];
				else
					w[to] = lft - siz[to];
			}
			sort(edge[newrt].begin(), edge[newrt].end(),
				 [&](int x, int y) { return w[x] > w[y]; });
		}

		else {
			vector<int> vis(n + 1, 0);

			q1 = edge[newrt][0], q2 = edge[newrt][1];
			cout << "? " << q1 << " " << q2 << endl;
			int x;
			cin >> x;
			queue<int> q;

			lft = 0;

			if (x == 1) {
				if (edge[newrt].size() == 2) {
					cout << "! " << newrt << endl;
					return;
				}
				q.push(edge[newrt][2]);
				vis[q1] = vis[q2] = 1;
				newrt = edge[newrt][2];
				lft = -2;
			}

			if (x == 0) {
				// q1
				q.push(q1);
				vis[newrt] = 1;
				newrt = q1;
				lft = -1;
			}
			if (x == 2) {
				q.push(q2);
				vis[newrt] = 1;
				newrt = q2;
				lft = -1;
			}

			vector<vector<int>> edge1(n + 1);
			while (!q.empty()) {
				int x = q.front();
				// debug(x);
				q.pop();
				vis[x] = 1;
				for (auto to : edge[x]) {
					if (vis[to])
						continue;
					q.push(to);
					edge1[x].push_back(to);
					edge1[to].push_back(x);
				}
			}

			edge.clear();
			edge = edge1;


			rt = newrt;


			for (int i = 1; i <= n; i++) {
				lft += vis[i];
			}
		}

		// FINISH
	}

	for (int i = 1; i <= n; i = (i + 1) % n + 1) {
		for (auto to : edge[i]) {
			i = to;
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int T = 1;
	cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}

詳細信息

Test #1:

score: 0
Time Limit Exceeded

input:

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

output:


result: